Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with this C programming assignment. This is what i have so far and its not working 100% could someone help fix it

I need help with this C programming assignment. This is what i have so far and its not working 100% could someone help fix it for me.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Codes:

#include

#include

#include

#include

pthread_barrier_t barrier;

int *arr,*sum;

pthread_mutex_t lock;

struct index

{

int i;

int j;

};

void *sum_func(void *arg)

{

struct index *st=(struct index*)arg;

printf("adding %d %d ",st->i,st->j);

pthread_mutex_lock(&lock);

sum[st->j]=sum[st->j]+sum[st->j-st->i];

pthread_mutex_unlock(&lock);

pthread_barrier_wait(&barrier);

pthread_exit(NULL);

}

int main()

{

pthread_attr_t tattr;

int i,j,k,l,count=0,number;

pthread_attr_init(&tattr);

pthread_attr_setscope(&tattr,PTHREAD_SCOPE_SYSTEM);

FILE *ftp=fopen("data","r+");

while(fscanf(ftp, "%d",&number) != EOF)

{

count++;

}

rewind(ftp);

printf("count = %d",count);

arr=(int*)malloc(sizeof(int)*count);

for(i=0;i

{

fscanf(ftp, "%d",&arr[i]);

}

for(j=0;j

{

printf("%d ",arr[j]);

sum[j]=arr[j];

}

printf(" ");

for(i=0;i

{

if(pthread_barrier_init(&barrier,NULL,(int)(count-(pow(2,i))))!=0)

{

printf(" barrier init has failed ");

return 1;

}

pthread_t tid[(int)(count-(pow(2,i)))];

printf(" i=%d ",i);

for(j=count-1;j>=pow(2,i);j--)

{

struct index st;

pthread_mutex_init(&lock, NULL) ;

st.i=pow(2,i);

st.j=j;

int r=pthread_create(&tid[j],&tattr,sum_func,(void*)&st);

printf(" %d %d : ",st.i,st.j);

if(r!=0)

{

printf(" thread creation has failed ");

return 1;

}

else

printf("thread %d created ",j);

}

//pthread_attr_destroy(&tattr);

for(k=0;k

pthread_join(tid[k],NULL);

for(l=0;l

{

printf("%d ",sum[l]);

}

printf(" ");

pthread_mutex_destroy(&lock);

pthread_barrier_destroy(&barrier);

//pthread_exit(NULL);

}

}

Objective: The goal of this homework is to get you familiar programming using POSIX threads. You will write a multithreaded program to compute in parallel the sums of all prefixes of an array A of n integers. This technique is called parallel prefix computation a The beauty of this technique is that it can be used for any associative binary operation (and not just for addition) and consequently it is useful in many different applications While implementing this technique, you will need to synchronize the threads using barrier synchronization mechanism. with multithreaded nd is explained in detail in the next section Parallel Prefix Computation: Assume that the array A is indexed from 1 to n and consists of n integers. The goal is create another array, called Sum of the same size and such that Sum [i] contains the sum of the first i elements of A. Of course this can be easily achieved by the following simple program fragment. Sum [1] -A[1] for (i-2; i 3. If you continue to double the distance, then afterlog.nl rounds you will have computed all partial sums (Can you prove it?) Objective: The goal of this homework is to get you familiar programming using POSIX threads. You will write a multithreaded program to compute in parallel the sums of all prefixes of an array A of n integers. This technique is called parallel prefix computation a The beauty of this technique is that it can be used for any associative binary operation (and not just for addition) and consequently it is useful in many different applications While implementing this technique, you will need to synchronize the threads using barrier synchronization mechanism. with multithreaded nd is explained in detail in the next section Parallel Prefix Computation: Assume that the array A is indexed from 1 to n and consists of n integers. The goal is create another array, called Sum of the same size and such that Sum [i] contains the sum of the first i elements of A. Of course this can be easily achieved by the following simple program fragment. Sum [1] -A[1] for (i-2; i 3. If you continue to double the distance, then afterlog.nl rounds you will have computed all partial sums (Can you prove it?)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions