I am solving the n Queens problem with open mp,
(The original eight queens problem consisted of trying to find a way to place eight queens on a chessboard so that no queen would attack any other queen. An alternate way of expressing the problem is to place eight “anythings” on an eight by eight grid such that none of them share a common row, column, or diagonal.)
If you look at the code I tried using #pragma omp task
but seems to go to a forever loop, How would you use omp task in the solve(int Queens[])
function?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <omp.h>
#define N 15
#define MAXThreads 2
int solutions;
int put(int Queens[], int row, int column)
{
int i;
//#pragma for schedule(static)
for(i=0; i<row; i++) {
if (Queens[i] == column || abs( Queens[i]-column ) == ( row-i ) )
return -1;
}
Queens[row]=column;
if(row==N-1) {
#pragma omp critical
{
solutions++;
}
}else{
for(i=0; i<N; i++) { // increment row
put(Queens, row+1, i);
}
}
return 0;
}
void solve(int Queens[]) {
int i;
#pragma omp parallel private(Queens) num_threads(MAXThreads)
{
// #pragma omp single
// {
#pragma omp for schedule(static)
for(i=0; i<N; i++) {
// #pragma omp task
put(Queens, 0, i);
// }
}
}
}
int main()
{
int Queens[N];
time_t t0=0, tf=0,t0s=0,tfs=0;
//------------------------------------------
t0 = time(NULL);
//------------------------------------------
//solve_secuencial(Queens);
solve(Queens);
//------------------------------------------
tf = time(NULL);
//------------------------------------------
printf( "# solutions %d time: %f \n",solutions, difftime(tf, t0));
return 0;
}