Present your own fully documented and tested programming example illustrating the prevention of a data race in a parallelised program.
This is an example where total number of prime number between 2 and 10000 is being calculated. The order of memory accesses is prevented from being non-deterministic and from the code data race is avoided by making it parallelized with OpenMP directives. The inconsistency can be seen by running the program consecutively for 3 or more times.
#include
#define MyThreads 4
#define num 10000
int primeNum[num];
int primeF[num];
int is_prime(int v)
{
int i;
int lim = floor(sqrt(v)) + 1;
for (i = 2; i < lim; i++) {
/* no need to check against known composites */
if (!primeF[i])
continue;
if (v % i == 0) {
primeF[v] = 0;
return 0;
}
return (v > 1);
int main(int argn, char **argv)
int total = 0;
printf("Enter a number greater than 2");
scanf("%d", &num)
#ifdef _OPEnumMP
omp_set_dynamic(0);
omp_set_num_threads(MyThreads);
#endif
for (i = 0; i < num; i++) {
primeF[i] = 1;
#pragma omp parallel for
for (i = 2; i < num; i++) {
if ( is_prime(i) ) {
primeNum[total] = i;
total++;
printf("total prime numbers that lie between 2 and %d: %d\n",
num, total);