简体   繁体   English

为什么我看不到执行for循环的混合线程

[英]Why don't I see a mix of threads executing the for loop

I have set the number of threads to 4. 我将线程数设置为4。

My problem is I see a sequential type behavior in the following code. 我的问题是在以下代码中看到了顺序类型的行为。 I would like to see all threads print their id in random ways, which is how a pthread implementation would behave. 我希望看到所有线程以随机方式打印其ID,这就是pthread实现的行为。

#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#define CHUNKSIZE 5
#define N 1000

int main (int argc, char *argv[]) 
{
   int nthreads, tid, i, chunk;
   float a[N], b[N], c[N];

   /* Some initializations */
   for (i=0; i < N; i++)
     a[i] = b[i] = i * 1.0;
   chunk = CHUNKSIZE;

   #pragma omp parallel for schedule(static,10) shared(a,b,c) private(i,tid)
   for (i=0; i<N; i++)
   {
      tid = omp_get_thread_num();
      c[i] = a[i] + b[i];
      printf("Thread %d: c[%d]= %f\n",tid,i,c[i]);
   }
}

Output from a section: 部分的输出:

Thread 2: c[949]= 1898.000000
Thread 2: c[980]= 1960.000000
Thread 2: c[981]= 1962.000000
Thread 2: c[982]= 1964.000000
Thread 2: c[983]= 1966.000000
Thread 2: c[984]= 1968.000000
Thread 2: c[985]= 1970.000000
Thread 2: c[986]= 1972.000000
Thread 2: c[987]= 1974.000000
Thread 2: c[988]= 1976.000000
Thread 2: c[989]= 1978.000000
Thread 0: c[1]= 2.000000
Thread 0: c[2]= 4.000000
Thread 0: c[3]= 6.000000
Thread 0: c[4]= 8.000000
Thread 0: c[5]= 10.000000
Thread 0: c[6]= 12.000000
Thread 0: c[7]= 14.000000
Thread 0: c[8]= 16.000000
Thread 0: c[9]= 18.000000
Thread 0: c[40]= 80.000000
Thread 0: c[41]= 82.000000
Thread 0: c[42]= 84.000000
Thread 0: c[43]= 86.000000
Thread 0: c[44]= 88.000000
Thread 0: c[45]= 90.000000
Thread 0: c[46]= 92.000000
Thread 0: c[47]= 94.000000
Thread 0: c[48]= 96.000000
Thread 0: c[49]= 98.000000
Thread 0: c[80]= 160.000000
Thread 0: c[81]= 162.000000
Thread 0: c[82]= 164.000000
Thread 0: c[83]= 166.000000
Thread 0: c[84]= 168.000000
Thread 0: c[85]= 170.000000
Thread 0: c[86]= 172.000000
Thread 0: c[87]= 174.000000
Thread 0: c[88]= 176.000000
Thread 0: c[89]= 178.000000
Thread 0: c[120]= 240.000000
Thread 0: c[121]= 242.000000
Thread 0: c[122]= 244.000000
Thread 0: c[123]= 246.000000

Is your machine single-core? 您的机器是单核吗? If so, threads are probably spawned sequentially, and your OS uses round-robin scheduling to execute each thread. 如果是这样,线程可能会顺序产生,并且您的操作系统使用循环调度来执行每个线程。 You'll only see interleaved threads on multi-core machines. 您只会在多核计算机上看到交错的线程。

EDIT: 编辑:

In line with my comment below, I think what you want to see is randomness in which thread executes, which is not the case depending on your OS scheduler. 与下面的评论一致,我认为您希望看到的是线程执行的随机性,实际情况并非如此,具体取决于您的OS调度程序。

It's not really useful to do multithreading on a single core machine. 在单核计算机上执行多线程并不是真的有用。 Parallelism to two threads might give you a small performance boost, but you'll have diminishing returns, and overhead of forks and joins will quickly make performance drop. 对两个线程的并行处理可能会给您带来一点性能提升,但是收益将递减,并且派生和联接的开销将很快使性能下降。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM