Nqueens 与 openmp c
我正在用 open mp 解决 n 个皇后问题,
(最初的八个皇后问题包括试图找到一种方法将八个皇后放在棋盘上,这样没有皇后会攻击任何其他皇后。表达该问题的另一种方法是将八个“任何东西”放在八乘八的网格上,这样它们就不会共享公共行、列或对角线。)
如果您查看我尝试使用#pragma omp task
的代码,但似乎进入永远循环,如何在 solve(int Queens[])
函数中使用 omp 任务?
#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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
自从我上次使用 OpenMP 以来已经有一段时间了,但我相信
private(p)
(其中p
是一个指针(或数组参数))只会使 指针,不是所指向的数据,私有。所以数组本身仍然是共享的。请参阅此问题。It's been a while since last I used OpenMP, but I believe that
private(p)
, wherep
is a pointer (or array parameter) will only make the pointer, not the data being pointed to, private. So the array itself is still shared. See this question.