I have a simple C program for N-queens calculation. I parallelized it using OpenMP. Now I want to execute both the serial and parallel versions and calculate the speed up. The point is that I don't want to create a new file for the serial code, or just copy my solution to a new function without the OpenMP directive. What I want to do is, keep one function, and tell from the main when to execute it as a serial and when as a parallel. I though of using preprocessors but I'm sure whether it is possible, and if yes, how can I achieve it.
void solve()
{
int i;
#if PARALLEL == 1
#pragma omp parallel for
#endif
for(i = 0; i < size; i++) {
int *queens = (int*)malloc(sizeof(int)*size);
setQueen(queens, 0, i);
free(queens);
}
}
int main()
{
...
#define PARALLEL 0
st_start = clock();
solve();
st_end = clock();
#define PARALLEL 1
pt_start = omp_get_wtime();
solve();
pt_end = omp_get_wtime();
...
}
Unluckily, you can't do it like that.
Preprocessor just scans over your code and REPLACES the #stuff. After this is done, the compiler compiles the code and there's nothing with #this
So at the code you posted, the preprocessor starts at the first line, does the #pragma stuff to code if PARALLEL is 1 and then continues at main, defining PARALLEL to 0 and then to 1.
It does NOT start at main and then get into solve();
You might want to take a look at OpenMP: conditional use of #pragma
You could try
I haven't tried this code and I'm not experienced with OMP, though...