繁体   English   中英

pthread并发运行线程c

[英]pthread running thread concurrently c

我需要在c中使用pthread制作Leibniz算法,现在我有了这个代码,但是目前线程实现需要顺序实现的同一时间,我认为它不是并发运行的。 有人可以看到错误。

谢谢!!

#include<stdio.h>
#include<math.h>
#include<pthread.h>
#include<stdlib.h>
#define NUM_THREADS 2
#define ITERATIONS 100000000
double result = 0.0;
void *leibniz(void *threadid){
  int size = ITERATIONS/NUM_THREADS;
  int start = (long)threadid * size;
  int end = ((long)threadid+1) * size;
  int i;
  for(i = start; i<end; i++){
    int denom = 2*i+1;
    result += pow(-1.0, i) * (1.0/denom);
  }
}

int main(){
  pthread_t threads[NUM_THREADS];
  long t;
  int rc;

  // CREATE
  for(t=0;t<NUM_THREADS;t++){
    rc = pthread_create(&threads[t], NULL, leibniz, (void *)t);
    if(rc){
      printf("ERROR: return code %d\n", rc);
    }
  }
  // JOIN
  for(t=0;t<NUM_THREADS;t++){
    rc = pthread_join(threads[t], NULL);
    if(rc){
      printf("ERROR: return code %d\n", rc);
      exit(-1);
    }
  }
  printf("Pi %f\n", result*4);
  exit(0);

}

感谢Jean-FrançoisFabre,我做了这些改变,现在它可以了!

double result=0.0;

void *leibniz(void *threadid){
  double local = 0.0;
  int size = ITERATIONS/NUM_THREADS;
  int start = (long)threadid * size;
  int end = ((long)threadid+1) * size;
  int i;
  for(i = start; i<end; i++){
    local += (i%2==0 ? 1 : -1) * (1.0/(2*i+1));
  }
  result += local*4;
}

我会尝试回答。

即使您的应用程序是多线程的,也不能保证每个核心有1个FPU。 我对此知之甚少,但我认为有些AMD处理器实际上在内核之间共享 FPU。

由于你的循环基本上是添加和pow ,它是99%的FPU计算,所以如果FPU在你的计算机上共享,它就解释了瓶颈。

您可以通过不调用pow来计算-11来减少FPU的使用,这将是一个标量操作,并且可能会产生影响。 如果i是奇数,则使用-1 ,否则使用1 ,或者在每次迭代时使用外部1 / -1变量。

另外,为了避免竞争条件,将结果累积到本地结果中,并将其添加到最后(最后通过互斥体保护添加会更好)

double result = 0.0;
void *leibniz(void *threadid){
  double local = 0.0;
  int size = ITERATIONS/NUM_THREADS;
  int start = (long)threadid * size;
  int end = ((long)threadid+1) * size;
  int i;
  for(i = start; i<end; i++){
    int denom = 2*i+1;
    // using a ternary/scalar speeds up the "pow" computation, multithread or not
    local += (i%2 ? -1 : 1) * (1.0/denom);
  }
  // you may want to protect that addition with a pthread_mutex
  // start of critical section
  result += local;
  // end of critical section
}

http://wccftech.com/amd-one-fpu-per-core-design-zen-processors/

我在Windows上运行Visual Studio,并且我没有安装pthread,因此我使用Windows线程创建了一个测试程序。 我将计算分为一个计算所有正项的函数和一个计算所有负项的函数。 双精度不是问题,因为正和<22,负和> -19。

处理器是英特尔3770K 3.5ghz(每个核心都有自己的FPU)。 我测试连续调用两个函数而不是为第二个函数使用单独的线程,并且两个线程的情况是单线程情况的两倍,单线程~0.360秒,双线程〜= 0.180秒。

#include <stdio.h>
#include <time.h>
#include <windows.h>

static HANDLE ht1;                      /* thread handle */

static DWORD WINAPI Thread0(LPVOID);    /* thread functions */
static DWORD WINAPI Thread1(LPVOID);

static clock_t ctTimeStart;             /* clock values */
static clock_t ctTimeStop;
static double  dTime;

static double pip;              /* sum of positive terms */
static double pim;              /* sum of negative terms */
static double pi;               /* pi */

int main()
{
    ctTimeStart = clock();
    Thread0(NULL);
    Thread1(NULL);
    ctTimeStop = clock();
    dTime = (double)(ctTimeStop - ctTimeStart) / (double)(CLOCKS_PER_SEC);  
    pip *= 4.;          /* pip <  22 after *= 4. */
    pim *= 4.;          /* pim > -19 after *= 4. */
    pi = pip + pim;
    printf("%.16lf %.16lf %.16lf %2.5lf secs\n", pi, pip, pim, dTime);

    ctTimeStart = clock();
    ht1 = CreateThread(NULL, 0, Thread1, 0, 0, 0);
    Thread0(NULL);
    WaitForSingleObject(ht1, INFINITE); // wait for thead 1
    ctTimeStop = clock();
    dTime = (double)(ctTimeStop - ctTimeStart) / (double)(CLOCKS_PER_SEC);  
    pip *= 4.;          /* pip <  22 after *= 4. */
    pim *= 4.;          /* pim > -19 after *= 4. */
    pi = pip + pim;
    printf("%.16lf %.16lf %.16lf %2.5lf secs\n", pi, pip, pim, dTime);

    CloseHandle(ht1);

    return 0;
}

DWORD WINAPI Thread0(LPVOID lpVoid)
{
double pp = 0.;                         /* local sum */
int j;
    for(j = 200000001; j >= 0; j -= 4)
        pp += 1. / (double)(j);
    pip = pp;                           /* store sum */
    return 0;
}

DWORD WINAPI Thread1(LPVOID lpVoid)
{
double pm = 0.;                         /* local sum */
int j;
    for(j = 200000003; j >= 0; j -= 4)
        pm -= 1. / (double)(j);
    pim = pm;                           /* store sum */
    return 0;
}

暂无
暂无

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

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