17

I have a for loop in my C code as follows:

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

    a[i] = simulate(); //  simulate() function simulates some system

}

We see that computation of each iteration is independent from others (the order of elements in a[] is not important to me). I want to parallelize the computation of this for loop using multi-threading. I am not exactly aware of how to do this in C? I have a 8 processor machine, so I can run 8 threads parallely.

3 Answers 3

18

There's no portable way to do parallelism in C*. However, the OpenMP standard is widely supported:

#pragma omp parallel for
for(i=0; i<100000; i++){

    a[i] = simulate(); //  simulate() function simulates some system

}

Depending on your compiler, there will be a flag that you must set to enable OpenMP support:

  • MSVC: /openmp
  • GCC: -fopenmp

as well as a header if you wish to access certain OpenMP functions:

#include <omp.h>

EDIT :

*The (very recently approved) C11 standard has support for threads via <threads.h>.

5
  • 1
    There's no portable way to do parallelism in C The new standard C11 is only days old now, but this is changing! Commented Dec 24, 2011 at 19:22
  • @kaizer.se Woah, I didn't realize C11 was approved! I'll mentioned that in my answer. Thanks!
    – Mysticial
    Commented Dec 24, 2011 at 19:24
  • Thanks for your reply. I tried this. For some a[i] I am getting a "nan" or "-nan" although the code is working properly when executed serially. I think there may be some kind of synchronization problem Commented Dec 26, 2011 at 15:52
  • Then, yes, you most likely have some sort of data-race that you need to sort out.
    – Mysticial
    Commented Dec 26, 2011 at 19:33
  • 1
    C11 thread support stackoverflow.com/questions/8859394/c11-thread-h-in-gcc Commented Nov 5, 2015 at 10:04
2

If your compiler supports the C11 standard, specifically stdatomic.h then you can do this.

Below is a crude example that should give you the basic idea behind it. It's not very hard. This one uses posix threads but you should be able to use any threading library.

#include <stdio.h>
#include <stdatomic.h>
#include <pthread.h>

#define ELEMENTS_N  500000

_Atomic unsigned int x;
unsigned int N;
unsigned int anyArray[ELEMENTS_N];

void * ThreadLoop ( void * args)
{
  unsigned int l;
  while( (l = atomic_load( &x )) < N ) 
  {
    if ( atomic_compare_exchange_weak( &x, &l, l + 1 ) )
    {
      anyArray[l] = l;
    }
  }
  return 0;
}


int main (int argc, char *argv[] )
{

  pthread_t th1;
  pthread_t th2;
  int v;

  atomic_store( &x, 0 );
  N = ELEMENTS_N;

  v = pthread_create(&th1, NULL, &ThreadLoop, NULL );
  v = pthread_create(&th2, NULL, &ThreadLoop, NULL );

  pthread_join( th1, NULL );
  pthread_join( th2, NULL );

  for(v = 0; v < ELEMENTS_N; v++ )
  {
    printf("%d ", anyArray[v] );
  }

  return 0;
}
0
// Finding minimum in a set using parallel CPU
int j,k;
double val_glob=1;
double val, valmin;
for (k=0; k<100; k++) {
  // This j-loop should run in parallel and each thread pick its own part of the k-values from the k-loop
  valmin=1;
  for (j=0; j<100000; j++)
    val=sin((double)k)*sin((double)j);
    if (val<valmin) {
      valmin=val;
    };
  };
  // This should run in serial
  if (valmin<val_glob) {
    val_glob=valmin;
  }
  // Here if k-loop is not finished next thread should start it again always with different k-value.
};
printf("Minimum is: %g\n",val_glob);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.