OpenMP implements producer and consumer models

Source: Internet
Author: User

The producer and consumer model is very old. I recently wrote an OpenMP version to share this model.

The general approach of the model is as follows:

1. The producer needs to take the task and produce the product.

2. The consumer needs to take the product and consume the product.


After the producer produces a product, it must inform the consumer that the product is ready for use. Consumers can use this signal to obtain the product and further consume the product.


For example, we have n images that need to be filtered or transformed for each image, and the processed results are stored on the hard disk.

Then the producer can regard n images as N tasks, and each task is independent. The computing result of each task can be regarded as a product, and the consumer writes the product to the hard disk.


Paste an instance code for explanation.

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#include <time.h>#define jobs 1000#define sz 102000#if defined(_WIN32) && defined(_MSC_VER)#include <windows.h>double abtic() {__int64 freq;__int64 clock;QueryPerformanceFrequency( (LARGE_INTEGER *)&freq );QueryPerformanceCounter( (LARGE_INTEGER *)&clock );return (double)clock/freq*1000*1000;}#else#include <time.h>#include <sys/time.h>double abtic() {double result = 0.0;struct timeval tv;gettimeofday( &tv, NULL );result = tv.tv_sec*1000*1000 + tv.tv_usec;return result;}#endif /* _WIN32 */#if 1double timer;#define ABTMS timer=abtic();fprintf(stdout,"%4d  ",__LINE__)#define ABTME fprintf(stdout,"%4d  %8.8fms\n",__LINE__,(abtic()-timer)/1000.0f)#else#define ABTMS #define ABTME #endifint main(){  char *jbNotReady;  double *a;  double *as;  double *pa;  int j, k;char jbnr;  a = (double*)malloc(sz*jobs*sizeof(double));  as = (double*)malloc(jobs*sizeof(double));  jbNotReady = (char*)malloc(jobs*sizeof(char));  for (j = 0; j < jobs; j++)  {    jbNotReady[j] = 1;      }  memset(a, 0, sz*jobs*sizeof(double));  memset(as, 0, jobs*sizeof(double));  ABTMS;#pragma omp parallel sections private(j,k,pa) shared(jbNotReady,as,a)  {    // producer#pragma omp section    {      for (j = 0; j < jobs; j++)      {        pa = a+j*sz;        for (k = 0; k < sz; k++)        {          pa[k] = 1.0;        }        jbNotReady[j] = 0;#pragma omp flush      }    }    // consumer#pragma omp section    {      for (j = 0; j < jobs; j++)      {#pragma omp flush        while (jbNotReady[j]){#pragma omp flush}        as[j] = 0.0;        pa = a+j*sz;        for (k = 0; k < sz; k++)        {          as[j] += pa[k];        }        if ((int)(as[j])!=sz)fprintf(stdout, "job id %3d :%f\n", j, as[j]);      }    }  }  ABTME;  free(a);  free(as);  free(jbNotReady);  return 0;}

In the source code, the thread created in the first section assumes the producer role, and the second section assumes the consumer role. J. This variable simulates the task number. The Loop Simulation in the first section produces the product. The second section obtains a task in sequence and verifies whether the task is prepared to obtain the correct product.

The flush guidance statement is used to forcibly keep the cache of each thread consistent with the memory. Note that the producer writes data to jbnotready, while the consumer only reads data and does not write the data in the memory, the data obtained by each thread is safe when writing data after reading.


The above Code supports both Windows and Linux, and can be executed in Versions later than gcc4.4. In Windows, it is feasible to support OpenMP compilers.




OpenMP implements producer and consumer models

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.