Demo entry 6771999

MISD_SISD

   

Submitted by anonymous on Nov 23, 2018 at 04:16
Language: C. Code size: 1.6 kB.

#include<stdio.h>
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<time.h>

#ifndef N
#define N (10000)
#endif

int a[N], b[N];
pthread_t thread[4];
int n;

typedef struct args{
	int i;
	int k;
}args;

args* make_args(int i, int k)
{
        args* current = (args*)malloc(sizeof(args)); 
        current->i = i;
        current->k = k;
        return current; 
}

void fill()
{
	//srand(time(NULL);
	for(int i = 0; i < N; i++)
	{
		a[i] = b[i] = 1; //rand()%N;
	}
}

void* mul(void* thread_args)
{
	int i = ((args*)thread_args)->i;
	int k = ((args*)thread_args)->k;
	a[i] *= k;
	k++;
	if((k-2) < 4)
		pthread_create(&thread[k-2], NULL, mul, (void*)make_args(i, k));
}

double MISD()
{
	void* res;
	int k = 2;
	clock_t start_MISD, end_MISD;
	start_MISD = clock();
	for(int i = 0; i < N; i++)
	{
		pthread_create(&thread[0], NULL, mul, (void*)make_args(i, k));
		pthread_join(thread[0], &res);
	}
	for(int i = 0; i < 4; i++)
		pthread_join(thread[i], &res);
	end_MISD = clock();
	double time = (double)(end_MISD - start_MISD) / CLOCKS_PER_SEC;
	return time;

}

double SISD()
{
	clock_t  start_SISD, end_SISD;
	start_SISD = clock();
	for(int i = 2; i < 6; i++)
		for(int j = 0; j < N; j++)
			b[j] *= i;
	end_SISD = clock();
	double time = (double)(end_SISD - start_SISD) / CLOCKS_PER_SEC;
	return time;
}

int main()
{
	fill();
	double MISD_time = MISD();
	double SISD_time = SISD();
	printf("MISD time: %f\n", MISD_time);
	printf("SISD time: %f\n", SISD_time);
	return 0;
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).