C 将源文件从动态调度更改为静态调度
我试图将下面的C源代码更改为静态调度,但我不知道它是如何完成的。在C 将源文件从动态调度更改为静态调度,c,multithreading,openmp,C,Multithreading,Openmp,我试图将下面的C源代码更改为静态调度,但我不知道它是如何完成的。在#pragma omp parallel private(nthreads,tid)之前,我尝试过静态: #包括 #包括 #包括 int main(int argc,char*argv[]) { 国际贸易署; /*分叉一组线程,给他们自己的变量副本*/ static#pragma omp parallel private(nthreads,tid)//我在这里试过 { /*获取线程数*/ tid=omp_get_thread_nu
#pragma omp parallel private(nthreads,tid)
之前,我尝试过静态
:
#包括
#包括
#包括
int main(int argc,char*argv[])
{
国际贸易署;
/*分叉一组线程,给他们自己的变量副本*/
static#pragma omp parallel private(nthreads,tid)//我在这里试过
{
/*获取线程数*/
tid=omp_get_thread_num();
printf(“Hello World from thread=%d\n”,tid);
/*只有主线程才能执行此操作*/
如果(tid==0)
{
nthreads=omp_get_num_threads();
printf(“线程数=%d\n”,n个线程);
}
}/*所有线程连接主线程并解散*/
}
我所期望的是线程0获取第一个块,线程1获取第二个块,依此类推。但是,它现在是随机的,因为它是动态的
#pragma-omp-parallel-private(nthreads,tid)调度(static)
当我试图编译它时,它说:'schedule'对#pragma-omp-parallel'#pragma-omp-parallel-private(nthreads,tid)调度(static)
糟糕,我没有注意到您没有用于循环的。附表
指令仅适用于omp for
指令。因此,本质上,您的调度问题对于您提供的代码片段没有任何意义现在它说:world.c:12:45:error:“for”必须是调度的“#pragma omp parallel”#pragma omp parallel private(nthreads,tid)的第一个子句(static)^world.c:12:45:错误:“for”对“#pragma omp parallel”世界无效。c:12:49:错误:“schedule”对“#pragma omp parallel”无效#pragma omp parallel private(nthreads,tid)对schedule(static)无效
^您需要在某个地方为
循环设置一个。您没有任何这样的循环来跨线程分发工作,因此无法定义分发工作的调度类型。在您的并行
区域内的某个地方添加一个循环,用#pragma omp for schedule(static)
装饰它,这样就可以了
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
int nthreads, tid;
/* Fork a team of threads giving them their own copies of variables */
static #pragma omp parallel private(nthreads, tid) //I tried it here
{
/* Obtain thread number */
tid = omp_get_thread_num();
printf("Hello World from thread = %d\n", tid);
/* Only master thread does this */
if (tid == 0)
{
nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}
} /* All threads join master thread and disband */
}