- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下用于 n 皇后拼图的 Pthreads 代码。它编译成功,但我得到了错误的答案。无论我输入什么值,我都会得到答案零。我想我的代码中有某种逻辑错误。我猜问题发生在回溯/递归部分的某个地方。如果有人能给我建议一个解决方案,我会很高兴。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include <assert.h>
int NTHREADS, SIZE;
int *hist;
int count = 0;
int solve(int col, int tid)
{
int start = tid * SIZE/NTHREADS;
int end = (tid+1) * (SIZE/NTHREADS) - 1;
int i, j;
if (col == SIZE)
{
count++;
}
#define attack(i, j) (hist[j] == i || abs(hist[j] - i) == col - j)
for (i = start; i <= end; i++) {
for (j = 0; j < col && !attack(i, j); j++);
if (j < col) continue;
hist[col] = i;
solve(col + 1, tid);
}
return count;
}
void *worker(void *arg)
{
int tid = (int)arg;
solve(0, tid);
}
int main(int argc, char* argv[])
{
pthread_t* threads;
int rc, i;
// checking whether user has provided the needed arguments
if(argc != 3)
{
printf("Usage: %s <number_of_queens> <number_of_threads>\n", argv[0]);
exit(1);
}
// passing the provided arguments to the SIZE and NTHREADS
// variable, initializing matrices, and allocating space
// for the threads
SIZE = atoi(argv[1]);
NTHREADS = atoi(argv[2]);
threads = (pthread_t*)malloc(NTHREADS * sizeof(pthread_t));
hist = (int*)malloc(SIZE * sizeof(int));
// declaring the needed variables for calculating the running time
struct timespec begin, end;
double time_spent;
// starting the run time
clock_gettime(CLOCK_MONOTONIC, &begin);
for(i = 0; i < NTHREADS; i++) {
rc = pthread_create(&threads[i], NULL, worker, (void *)i);
assert(rc == 0); // checking whether thread creation was successful
}
for(i = 0; i < NTHREADS; i++) {
rc = pthread_join(threads[i], NULL);
assert(rc == 0); // checking whether thread join was successful
}
// ending the run time
clock_gettime(CLOCK_MONOTONIC, &end);
// calculating time spent during the calculation and printing it
time_spent = end.tv_sec - begin.tv_sec;
time_spent += (end.tv_nsec - begin.tv_nsec) / 1000000000.0;
printf("Elapsed time: %.2lf seconds.\n", time_spent);
printf("\nNumber of solutions: %d\n", count);
free(hist);
return 0;
}
最佳答案
确实有一些错误,但第一个错误确实导致全部为 0。请注意,如果仅使用一个线程,您的程序可以正常工作。
在solve
内部,您通过计算start
和end
来分割每个线程的工作,但是这不应该完成一次col > 0
,因为在递归中你必须再次遍历所有行。
您可以在线程之间共享变量 hist
和 count
,而无需在关键部分保护它们,例如使用信号量。在这种情况下,您可以不使用信号量,方法是为每个线程提供自己的这些变量版本,并累积线程连接中的所有 count
条目。共享count
会导致低概率的问题,但是共享仍然是错误的,因为你不能假设count++
是一个原子的读-修改-写操作。共享 hist
只是算法上的错误。
如果 NTHREADS
不能完全除以 SIZE
,则最后一个 end
的计算不一定会产生 SIZE-1
。
如果NTHREADS > SIZE
,您的start
和end
将始终为-1
。
作为良好实践,您应该始终检查 malloc
(和 friend )是否不返回 NULL
,因为这意味着您的内存不足。如果您检查命令行参数的正确性,这也是对程序用户的尊重。
如果你解决了这些错误,那么每次在常规大小的棋盘上运行它时,无论线程数是多少,你都会得到 92。祝你好运。
至于代码示例(下面询问),我有点不情愿,但是给你吧。就我个人而言,我会进行更多更改,但我尽可能接近您的代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include <assert.h>
int NTHREADS, SIZE;
int ** hist = NULL;
int * count = NULL;
static void oof(void)
{
fprintf(stderr, "Out of memory.\n");
exit(1);
}
void solve(int col, int tid)
{
/* If NTHREADS does not divide SIZE, you
* will not cover all rows.
* Also make sure SIZE >= NTHREADS, otherwise start is always 0.
*/
int start = (col > 0) ? 0 : tid * (SIZE/NTHREADS);
int end = (col > 0 || tid == NTHREADS-1) ? SIZE-1 : (tid+1) * (SIZE/NTHREADS) - 1;
int i, j;
if (col == SIZE)
{
count[tid]++;
}
#define attack(i, j) (hist[tid][j] == i || abs(hist[tid][j] - i) == col - j)
for (i = start; i <= end; i++) {
for (j = 0; j < col && !attack(i, j); j++);
if (j < col) continue;
hist[tid][col] = i;
solve(col + 1, tid);
}
}
void *worker(void *arg)
{
int tid = (int)arg;
count[tid] = 0;
solve(0, tid);
}
int main(int argc, char* argv[])
{
pthread_t* threads;
int rc, i, j, sum;
// checking whether user has provided the needed arguments
if(argc != 3)
{
printf("Usage: %s <number_of_queens> <number_of_threads>\n", argv[0]);
exit(1);
}
// passing the provided arguments to the SIZE and NTHREADS
// variable, initializing matrices, and allocating space
// for the threads
SIZE = atoi(argv[1]);
NTHREADS = atoi(argv[2]);
threads = (pthread_t*)malloc(NTHREADS * sizeof(pthread_t));
hist = malloc(SIZE * sizeof(int*));
count = malloc(SIZE * sizeof(int));
if (hist == NULL || count == NULL) oof();
for (i = 0; i < SIZE; i++)
{
hist[i] = malloc(SIZE * sizeof(int));
if (hist[i] == NULL) oof();
for (j = 0; j < SIZE; j++)
{
hist[i][j] = 0;
}
}
// declaring the needed variables for calculating the running time
struct timespec begin, end;
double time_spent;
// starting the run time
clock_gettime(CLOCK_MONOTONIC, &begin);
for(i = 0; i < NTHREADS; i++) {
rc = pthread_create(&threads[i], NULL, worker, (void *)i);
assert(rc == 0); // checking whether thread creation was successful
}
sum = 0;
for(i = 0; i < NTHREADS; i++) {
rc = pthread_join(threads[i], NULL);
assert(rc == 0); // checking whether thread join was successful
sum += count[i];
}
// ending the run time
clock_gettime(CLOCK_MONOTONIC, &end);
// calculating time spent during the calculation and printing it
time_spent = end.tv_sec - begin.tv_sec;
time_spent += (end.tv_nsec - begin.tv_nsec) / 1000000000.0;
printf("Elapsed time: %.2lf seconds.\n", time_spent);
printf("\nNumber of solutions: %d\n", sum);
for (i = 0; i < SIZE; i++)
{
free(hist[i]);
}
free(hist); free(count);
return 0;
}
关于c - 如何使用 Pthreads 并行化我的 n 皇后拼图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16114443/
我在某处遇到了以下难题 #include int main() { { /*Fill in something here to make this code compile
我正在尝试为 iOS 创建一个拼图游戏应用程序。从我在互联网上的搜索来看,确实没有任何关于这个主题的教程。有谁知道任何人都知道的好教程或游戏教程的链接?谢谢。顺便说一下,iOS4 将不胜感激。 最佳答
如果必须使用 Promises,您会如何编写以下代码? 这个想法是,“私有(private)”方法 p1 调用一个执行异步操作的函数,然后,当异步调用的结果准备就绪时,控制权将传递给“私有(priva
下面是其中一个 facebook 谜题:我无法理解如何进行此操作。 你有 C 个容器、B 个黑球和无限数量的白球。您希望以一种方式在容器之间分配球,即每个容器至少包含一个球,并且选择白球的概率大于或等
有 5 位成员围坐在一张 table 旁。关键值是坐在 table 周围的成员数量。所以现在关键值将是 5。一个恐怖分子告诉成员,因为你们是 5 个成员,所以我将从第一个成员开始数,数到 5 的人将被
你能在不抛出错误的情况下解决这个问题吗?答案是单线。这是来自一个死的职位发布,在回复中要求回答。我认为这是剔除受访者的聪明方法,但我似乎无法在不出错的情况下回答它。 显而易见的解决方案: f.moo(
此源输出 G'Day Mate. 这是怎么发生的? public static void main(String args[]) { System.out.println("Hello Wor
我正在 android 中开发一个 slider 拼图,它有一个图像被分解成小图像,我们需要对这些 fragment 进行排序以形成正确的图像。我使用了一个 3x3 的 GridView ,其中包含
我遇到了以下难题,无法在 Picat 中制定解决方案: You will generate 5-digit numbers, where each digit is in 1..5 and diffe
我是 Javascript 新手,并且正在努力解决 CodeWars 中的这个难题。 约翰想用壁纸装饰房间。房间的尺寸为:宽度(w)、高度(h)、长度(l)。一卷壁纸的尺寸为 52cm 宽,10m 长
我对 Java 还很陌生,尝试过 Best Before puzzle from Spotify昨天。当我发送它时,我收到“错误答案”错误。检查其他解决方案没有帮助,我无法弄清楚哪个输入给出了错误的答
我正在尝试恢复我拥有的一些旧代码,这是一个拼图游戏。它从文件夹中加载图像(拼图),将它们随机放置在页面周围,然后拖放到板上。这曾经有效,但当我今天尝试使用它时,它只是抛出错误(见下文)。 HTML:
这对你们来说可能是个愚蠢的问题。它是关于 CSS Sprites 的。我有一个包含 4 个菜单的导航,例如 .. HOME COMPANY SERVICES SUPPORT 尽管我使用了一个 css
我需要创建一个标题,可以根据正在构建的页面轻松添加或删除部分,但我在处理其中一部分时遇到了问题。 我有一个标题,看起来像这样将所有组件放在 如果导航被移除,它应该看起来像这样(垂直居中) 我的问题是如
我在 JS 中构建了一个 15 拼图,但我的随机拼图生成正在创建无法解决的拼图实例。这可能是因为我不是计算机科学专业的负责人,但我不确定如何计算代码排列中的反转次数。我想知道如何编写我的代码,以便我可
我正在寻找 8 Puzzle graphs tree generator,最好是 (php+) html+css+javascript。我需要的是类似 3 2 1 6 8 7 5 4 会生成所有可
我住在德国,在 Android Market 上发布“Last Call Widget”。随着时间的推移,我一直在稳步改进它,但一组用户仍然提示它无法在他们的设备上运行。 我的小部件监听“androi
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我正在尝试制作一个看起来像这样的拼图游戏。我试过的看起来像这样。 https://jsfiddle.net/uccfb46z/ 现在如果我想改变碎片的形状我需要修改这部分 - outside: fu
首先,让我为缺少 SSCCE 表示歉意。我在这方面真的没有足够的专业知识来弄清楚什么是相关的,什么不是。 简而言之,问题是在两台运行相同分辨率 (1366x768) 的不同计算机上,我女朋友的 tum
我是一名优秀的程序员,十分优秀!