并行化:线程结果不佳,流程结果良好。为什么?

时间:2013-05-04 22:37:58

标签: c multithreading performance parallel-processing

我遇到了一个问题,即C程序与线程的并行化并没有真正提高速度,而与进程的并行化实际上确实如此。我真的不明白为什么,所以也许有人可以解释。这里有两个程序,都计算平方根大约10.000.000次。首先是线程:

//clang  threads.c -Wall -O3 -o with_threads

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <pthread.h>

#define ENTRIES 10485760
#define THREADS 8

int threads_no[THREADS];
int current = 0;

void* squareroot(void* offset) {
  int foo = ENTRIES / current;
  float *a = malloc(sizeof(float)*foo);

  for (int i = 0; i < ENTRIES / current; i++)
    a[i] = i + 1;

  clock_t s0 = clock();

  int i = 0;
  while (i < ENTRIES / current) {
    a[i] = sqrtf(a[i]);
    ++i;
  }
  printf("Thread %d spent %f calculating %d entries\n", *(int*)offset, ((double)(clock() - s0) / CLOCKS_PER_SEC), i);
  return NULL;
}

int main() {

  for (int t = 0; t < THREADS; t++)
    threads_no[t] = t;

  while (++current <= THREADS) {
    printf("With %d threads...\n", current);

    pthread_t threads[current];

    for (int t = 0; t < current; t++)
      pthread_create(&threads[t], NULL, squareroot, &threads_no[t]);

    for (int t = 0; t < current; t++)
      pthread_join(threads[t], NULL);
  }
  return 0;
}

...以及与进程相对应的代码:

//clang  procs.c -Wall -O3 -o with_procs

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <math.h>

#define ENTRIES 10485760
#define PROCS 8

int procs[PROCS];
int current = 0;

void* squareroot(void* offset) {
  int foo = ENTRIES / current;
  float *a = malloc(sizeof(float)*foo);

  for (int i = 0; i < ENTRIES / current; i++)
    a[i] = i + 1;

  clock_t s0 = clock();

  int i = 0;
  while (i < ENTRIES / current) {
    a[i] = sqrtf(a[i]);
    ++i;
  }
  printf("Process %d spent %f calculating %d entries\n", *(int*)offset, ((double)(clock() - s0) / CLOCKS_PER_SEC), i);
  return NULL;
}

int main() {

  for (int t = 0; t < PROCS; t++)
    procs[t] = t;

  printf("Single:\n");
  current = 1;
  squareroot(&procs[0]);
  printf("Parallel:\n");
  current = 0;

  while (++current <= PROCS) {
    printf("Wiht %d procs...\n", current);

    for (int i = 0, pid = 0; i < current; i++) {
      pid = fork();
      if (pid < 0) {
        printf("Error");
        exit(1);
      } else if (pid == 0) {
        squareroot(&procs[i]);
        exit(0); 
      }
    }
    for (int i = 0; i < current; i++)
      wait(NULL);
  }
  return 0;
}

在我的机器上(MacBook Air Core i5 1,7),线程的结果是:

With 1 threads...
Thread 0 spent 0.030546 calculating 10485760 entries
With 2 threads...
Thread 1 spent 0.032468 calculating 5242880 entries
Thread 0 spent 0.037332 calculating 5242880 entries
With 3 threads...
Thread 0 spent 0.015804 calculating 3495253 entries
Thread 1 spent 0.026870 calculating 3495253 entries
Thread 2 spent 0.029845 calculating 3495253 entries
With 4 threads...
Thread 3 spent 0.037240 calculating 2621440 entries
Thread 0 spent 0.052195 calculating 2621440 entries
Thread 1 spent 0.056285 calculating 2621440 entries
Thread 2 spent 0.054233 calculating 2621440 entries
With 5 threads...
Thread 1 spent 0.026005 calculating 2097152 entries
Thread 3 spent 0.031361 calculating 2097152 entries
Thread 4 spent 0.041360 calculating 2097152 entries
Thread 2 spent 0.054898 calculating 2097152 entries
Thread 0 spent 0.034579 calculating 2097152 entries
With 6 threads...
Thread 2 spent 0.026277 calculating 1747626 entries
Thread 4 spent 0.029041 calculating 1747626 entries
Thread 1 spent 0.028271 calculating 1747626 entries
Thread 3 spent 0.018770 calculating 1747626 entries
Thread 5 spent 0.043817 calculating 1747626 entries
Thread 0 spent 0.019002 calculating 1747626 entries
With 7 threads...
Thread 0 spent 0.022857 calculating 1497965 entries
Thread 3 spent 0.050611 calculating 1497965 entries
Thread 5 spent 0.015109 calculating 1497965 entries
Thread 4 spent 0.028377 calculating 1497965 entries
Thread 1 spent 0.043619 calculating 1497965 entries
Thread 2 spent 0.071591 calculating 1497965 entries
Thread 6 spent 0.022199 calculating 1497965 entries
With 8 threads...
Thread 2 spent 0.039933 calculating 1310720 entries
Thread 5 spent 0.021614 calculating 1310720 entries
Thread 7 spent 0.062763 calculating 1310720 entries
Thread 3 spent 0.041014 calculating 1310720 entries
Thread 0 spent 0.033286 calculating 1310720 entries
Thread 6 spent 0.044050 calculating 1310720 entries
Thread 4 spent 0.082030 calculating 1310720 entries
Thread 1 spent 0.016579 calculating 1310720 entries

和流程:

Single:
Process 0 spent 0.030531 calculating 10485760 entries
Parallel:
Wiht 1 procs...
Process 0 spent 0.030548 calculating 10485760 entries
Wiht 2 procs...
Process 0 spent 0.015946 calculating 5242880 entries
Process 1 spent 0.015995 calculating 5242880 entries
Wiht 3 procs...
Process 1 spent 0.012040 calculating 3495253 entries
Process 0 spent 0.014993 calculating 3495253 entries
Process 2 spent 0.016536 calculating 3495253 entries
Wiht 4 procs...
Process 1 spent 0.009256 calculating 2621440 entries
Process 2 spent 0.011725 calculating 2621440 entries
Process 0 spent 0.008604 calculating 2621440 entries
Process 3 spent 0.011057 calculating 2621440 entries
Wiht 5 procs...
Process 0 spent 0.007498 calculating 2097152 entries
Process 1 spent 0.008804 calculating 2097152 entries
Process 4 spent 0.008814 calculating 2097152 entries
Process 3 spent 0.010208 calculating 2097152 entries
Process 2 spent 0.009060 calculating 2097152 entries
Wiht 6 procs...
Process 1 spent 0.005633 calculating 1747626 entries
Process 2 spent 0.005553 calculating 1747626 entries
Process 0 spent 0.005950 calculating 1747626 entries
Process 4 spent 0.005977 calculating 1747626 entries
Process 3 spent 0.009157 calculating 1747626 entries
Process 5 spent 0.009563 calculating 1747626 entries
Wiht 7 procs...
Process 4 spent 0.005060 calculating 1497965 entries
Process 0 spent 0.005710 calculating 1497965 entries
Process 1 spent 0.004703 calculating 1497965 entries
Process 3 spent 0.005091 calculating 1497965 entries
Process 6 spent 0.007243 calculating 1497965 entries
Process 5 spent 0.004760 calculating 1497965 entries
Process 2 spent 0.005729 calculating 1497965 entries
Wiht 8 procs...
Process 0 spent 0.005995 calculating 1310720 entries
Process 1 spent 0.004285 calculating 1310720 entries
Process 2 spent 0.006809 calculating 1310720 entries
Process 7 spent 0.005404 calculating 1310720 entries
Process 3 spent 0.005978 calculating 1310720 entries
Process 5 spent 0.004108 calculating 1310720 entries
Process 6 spent 0.005336 calculating 1310720 entries
Process 4 spent 0.005409 calculating 1310720 entries

对于线程,总是至少有一个线程占用一次,所以没有任何改进。流程似乎更好地平衡。我没有为线程使用任何同步原语,因为它们不是必需的。有人可以解释为什么他们如此不同?我搜索谷歌很长一段时间没有运气。

提前致谢。

更新:考虑到评论后,使用gettimeofday/2来衡量时间,线程实现似乎是正确的。供参考:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <pthread.h>
#include <sys/time.h>

#define ENTRIES 10485760
#define THREADS 8

int threads_no[THREADS];
int current = 0;

void* squareroot(void* offset) {
  int foo = ENTRIES / current;
  float *a = malloc(sizeof(float)*foo);

  for (int i = 0; i < ENTRIES / current; i++)
    a[i] = i + 1;

  clock_t s0 = clock();

  int i = 0;
  while (i < ENTRIES / current) {
    a[i] = sqrtf(a[i]);
    ++i;
  }
  // printf("Thread %d spent %f calculating %d entries\n", *(int*)offset, ((double)(clock() - s0) / CLOCKS_PER_SEC), i);
  return NULL;
}

int main() {

  for (int t = 0; t < THREADS; t++)
    threads_no[t] = t;

  struct timeval t1, t2;
  double elapsedTime;

  // start timer


  while (++current <= THREADS) {
    printf("With %d threads... ", current);
    gettimeofday(&t1, NULL);
    pthread_t threads[current];

    for (int t = 0; t < current; t++)
      pthread_create(&threads[t], NULL, squareroot, &threads_no[t]);

    for (int t = 0; t < current; t++)
      pthread_join(threads[t], NULL);
    gettimeofday(&t2, NULL);
    elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;      // sec to ms
    elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;   // us to ms
    printf("%f\n", elapsedTime);
  }
  return 0;
}

最佳, 马丁

3 个答案:

答案 0 :(得分:5)

clock测量处理时间,而不是线程时间。它对于测量单个螺纹的性能毫无用处。

答案 1 :(得分:3)

我认为这可能与clock()电话有关。 在我的系统中(没有-O3和8倍的数据),我得到了以下内容:

With 1 threads...
Thread 0 spent 2.390000 calculating 83886080 entries
With 2 threads...
Thread 0 spent 2.390000 calculating 41943040 entries
Thread 1 spent 2.380000 calculating 41943040 entries
With 3 threads...
Thread 0 spent 2.380000 calculating 27962026 entries
Thread 1 spent 2.370000 calculating 27962026 entries
Thread 2 spent 2.370000 calculating 27962026 entries
With 4 threads...
Thread 0 spent 2.370000 calculating 20971520 entries
Thread 2 spent 2.380000 calculating 20971520 entries
Thread 3 spent 2.260000 calculating 20971520 entries
...
With 7 threads...
Thread 1 spent 2.370000 calculating 11983725 entries
Thread 4 spent 2.340000 calculating 11983725 entries
Thread 0 spent 2.340000 calculating 11983725 entries
Thread 6 spent 2.340000 calculating 11983725 entries
....
With 8 threads...
Thread 1 spent 2.320000 calculating 10485760 entries
Thread 0 spent 2.330000 calculating 10485760 entries
Thread 5 spent 2.350000 calculating 10485760 entries
....
Thread 3 spent 2.060000 calculating 10485760 entries

现在,查看clock()手册页,它说:

On several other implementations, the value returned by clock() also includes
the times of any children whose status has been collected via wait(2) (or 
another wait-type call).
Linux does not include the times of waited-for children in the value returned by
clock(). The times(2) function, which explicitly returns (separate) information 
about the caller and its children, may be preferable.

那么也许这是与时间相关的问题?

P.S。在我的测试中,加速非常明确。

答案 2 :(得分:1)

你真的打算让'当前'成为全球性的吗?当其他线程正在使用它时,你正在改变它。