Question

I must execute in C using parallel Programming techniques the following serial program: void producer_consumer(int *buffer,...

I must execute in C using parallel Programming techniques the following serial program:

void producer_consumer(int *buffer, int size, int *vec, int n) {
   int i, j;
   long long unsigned int sum = 0;
   for(i=0;i<n;i++) {
       if(i % 2 == 0) {   // PRODUCER
           for(j=0;j<size;j++) {
               buffer[j] = vec[i] + j*vec[i+1];
           }
       }
       else {   // CONSUMER
           for(j=0;j<size;j++) {
               sum += buffer[j];
           }
       }
   }
   printf("%llu\n",sum);
}

HEADS UP:
All threads must alternate between producers and consumers among the iterations. In other words, when the iteration in the main loop is even, the thread is a producer, otherwise, a consumer.

The producer shall receive a value as an input reference, compute and store the result into a buffer in which the consumer will read in the next iteration. Consequently, the consumer shall read all computed values and sum them.


PROBLEM DESCRIPTION:
You must parallelize the above serial algorithm using OpenMP Pragmas. The program shall receive as an input 4 lines: The first one containing an integer which represents the number of threads that the program will eventually be executed. The second line contains a N integer, ALWAYS EVEN, which is the number of iteration in the main loop, it means, how many cycles of consumer-producer might occur, the third line contains an integer which is the buffer size, and the 4th line the N values used as a database for calculating the algorithm. THE VALUES MUST BE READ FROM A STANDARD INPUT (stdin) USING scanf() as following:

2 -> Number of threads
10 -> Cycles Producer + Cycles Consumer in the main loop
10 -> Buffer size
6 4 8 4 5 3 2 1 6 7 -> Values

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Given function producer consumer ) code used as it is without any changes Solution include required header files #inclu

Copyable code:

// include required header files

#include <stdio.h>

#include <stdlib.h>

#include <omp.h>

// Given producer_consumer function code

void producer_consumer(int *buffer, int size, int *vec, int n)

{

    int i, j;

    long long unsigned int sum = 0;

    for(i=0;i<n;i++) {

        if(i % 2 == 0) {    // PRODUCER

            for(j=0;j<size;j++) {

                buffer[j] = vec[i] + j*vec[i+1];

            }

        }

        else {    // CONSUMER

            for(j=0;j<size;j++) {

                sum += buffer[j];

            }

        }

    }

    printf("%llu\n",sum);

}

// main method to execute the given producer_consumer function code

int main ()

{

     // Declare the requierd variables

    int nm_thrds, cyls, buf_size, *buffr=NULL, *vectr=NULL;

    int lp;

    double stTme;

printf("Enter the value for number of threads, cycle, and buffer size:");

    scanf("%d %d %d", &nm_thrds, &cyls, &buf_size);

    buffr = (int *) malloc (buf_size * sizeof(int));

    vectr = (int *) malloc (buf_size * sizeof(int));

    if (buffr == NULL) {

        fprintf (stderr, "Could not allocate buffer\n");

        exit (1);

    }

    for (lp = 0; lp < buf_size; lp++) {

        scanf ("%d", &vectr[lp]);

    }

    // set the number o threads specified in input

    omp_set_num_threads(nm_thrds);

    stTme = omp_get_wtime();

    producer_consumer(buffr, cyls, vectr, buf_size);

    printf("%lf\n", omp_get_wtime() - stTme);

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
I must execute in C using parallel Programming techniques the following serial program: void producer_consumer(int *buffer,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Program 2: Thread version int i = 100; char *buffer: void *tfuc(void *noarg) { int j...

    Program 2: Thread version int i = 100; char *buffer: void *tfuc(void *noarg) { int j = 0; printf("B:1-%d, j = %d\n",1,1); printf("B: I = %d, j = %d\n",1,1); j = 3; strcpy(buffer, "red"); Pthread exit(NULL); //print the string (show values of i, j) //print the string (show values of i,1) //copy the string "red" to buffer. int main(void) { pthread_t tid; //declaring vars int j = 1; buffer strcpy(malloc(100), "blue"); //Initialize buffer and copy the "blue" to it pthread_create(&tid,...

  • write a C/C++ program which simulates the Producer/Consumer program in Figure 5.16 with a buffer size...

    write a C/C++ program which simulates the Producer/Consumer program in Figure 5.16 with a buffer size of one thousand. Allow the Producer to generate one million items. Use ten consumers. The program needs to perform a normal exit process after all items are consumed. Both the Producer (singular) and Consumers are to be runs as separate processes generated via fork(). The program must us Linux semaphores. The program must clean up the semaphores used and zombies created before termination. Report...

  • operating system engineering , please read the question and solve on the given skeleton code ....

    operating system engineering , please read the question and solve on the given skeleton code . Write a multi-threaded program with Semaphores as counters and pthread_mutex_t mutex to solve the producer-consumer problem: A bounded buffer is simply a global integer array of size N (2) which can be accessed by multiple threads. • Create two types of threads - Producer (2) and Consumer (2). Producers will write to the buffer, and the consumers will read the buffer. In this scenario,...

  • Solve the Consumer/Producer problem using semaphores. A skeleton program (Save it as producer_consumer.c) is provided to...

    Solve the Consumer/Producer problem using semaphores. A skeleton program (Save it as producer_consumer.c) is provided to you: The main function creates 2 threads: consumer represents the consumer and executes the consume function, and producer represents the producer and executes the  produce function. You should declare and initialize 3 semaphores. Those semaphores should be used in the consume(..) and produce(...) functions. #include <stdio.h> #include <stdlib.h> #include <pthread.h> //compile and link with -pthread #define BUFFER_SIZE 10 int buffer[BUFFER_SIZE]; int in, out; int num;...

  • I need the pseudocode for this c program source code. #include<stdio.h> void printarray(int array[], int asize){...

    I need the pseudocode for this c program source code. #include<stdio.h> void printarray(int array[], int asize){ int i; for(i = 0; i < asize;i++) printf("%d", array[i]); printf("\n"); } int sum(int array[], int asize){ int result = 0; int i = 0; for(i=0;i < asize;i++){ result = result + array[i]; } return result; } int swap( int* pA,int*pB){ int result = 0; if(*pA > pB){ int tamp = *pA; *pA = *pB; *pB = tamp; result = 1; } return result;...

  • in c++ language 1.Re-write the following for loop statement by using a while loop statement: int...

    in c++ language 1.Re-write the following for loop statement by using a while loop statement: int sum = 0; for(int i=0;i<=1000;i++){                 sum+=i; } 1 (b). Following is the main () function of a program. The program asks a user to enter 150 integers and print the largest integer user entered. int main() {    int score, highest;             //missing portion of the program             return 0;    } 1(c). Find the missing portion of the following code, so the...

  • C programm , ´hello i need your help -Given the C program primes.c with the following main method: int main() { int num, res; char buffer[11]; bool finished = false; while (!finished)...

    C programm , ´hello i need your help -Given the C program primes.c with the following main method: int main() { int num, res; char buffer[11]; bool finished = false; while (!finished) { printf("Enter n > 0 or quit\n"); scanf("%10s", buffer); if (strcmp(buffer, "quit") == 0) { finished = true; } else { // Convert input to number and compute n-th prime num = atoi(buffer); if (num > 0) { res = nth_prime(num); printf("Prime #%d is %d\n", num, res); }...

  • *Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a se...

    *Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a series with N+1 terms.* --The series sum is partitioned in T non-overlapping partial sums, each computed by T separate child processes created with the fork() library function.* --This program demonstrates data parallelism and interprocess communication using pipes. Each child process could perform a (potentially) long computation on a separate CPU (or core). Depending on the computer architecture, the...

  • In C. // Extract the integer from the input string and convert to int int convert(char...

    In C. // Extract the integer from the input string and convert to int int convert(char *input){ // Declare int for result extracted from input int res = 0; // Declare int for sign of result // Declare two iterators // Declare a buffer for numeric chars // Set error to zero - no error found yet // Check for space in element 1 // Check for negative integer at element 2 // Loop to copy all numeric chars to...

  • Trying to debug a C program given to me. This is what I was given... //...

    Trying to debug a C program given to me. This is what I was given... // Program to read numeric elements (including decimals) into a 3X3 matrix and display them #include<stdio.h> int main(void) {    int size = 3, Matrix[size][size];    printf("Enter 9 elements of the matrix:\n") for (int i = 0, i <=size, i++}        for (int j = 0, j <= size, i++)            scan("%c", Matrix1[2][2]);    diplay(Matrix) float display(int Matrix1[][], int size) (   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT