我已经写了一些使用OpenMP并行课程的并行c代码。Openmp基本并行化
继承人片段
#include <stdio.h>
#include <time.h>
#include <math.h>
#define FALSE 0
#define TRUE 1
int count_primes_0(int);
int count_primes_1(int);
int count_primes_2(int);
int main(int argc, char *argv[]){
int n;
if (argc != 2){
printf("Incorrect Invocation, use: \nq1 N");
return 0;
} else {
n = atoi(argv[1]);
}
if (n < 0){
printf("N cannot be negative");
return 0;
}
printf("N = %d\n", n);
//omp_set_num_threads(1);
time_it(count_primes_0, n, "Method 0");
time_it(count_primes_1, n, "Method 1");
time_it(count_primes_2, n, "Method 2");
return 0;
}
int is_prime(int n){
for(int i = 2; i <= (int)(sqrt((double) n)); i++){
if ((n % i) == 0){
return FALSE;
}
}
return n > 1;
}
void time_it(int (*f)(int), int n, char *string){
clock_t start_clock;
clock_t end_clock;
double calc_time;
int nprimes;
struct timeval start_val;
struct timeval end_val;
start_clock = clock();
nprimes = (*f)(n);
end_clock = clock();
calc_time = ((double)end_clock - (double)start_clock)/CLOCKS_PER_SEC;
printf("\tNumber of primes: %d \t Time taken: %fs\n\n", nprimes, calc_time);
}
// METHOD 0
// Base Case no parallelization
int count_primes_0(int n){
int nprimes = 0;
for(int i = 1; i <= n; i++){
if (is_prime(i)) {
nprimes++;
}
}
return nprimes;
}
//METHOD 1
// Use only For and Critical Constructs
int count_primes_1(int n){
int nprimes = 0;
#pragma omp parallel for
for(int i = 1; i <= n; i++){
if (is_prime(i)) {
#pragma omp critical
nprimes++;
}
}
return nprimes;
}
//METHOD 2
// Use Reduction
int count_primes_2(int n){
int nprimes = 0;
#pragma omp parallel for reduction(+:nprimes)
for(int i = 1; i <= n; i++){
if (is_prime(i)) {
nprimes++;
}
}
return nprimes;
}
我现在面临的问题是,当我使用OMP_SET_NUM_THREADS()少的线程我用 更快我的功能运行 - 或者更接近基地的运行时间并行化的情况下
时间结果: 方法0: 这些一个8芯机
8个线程上运行0.07s;方法1:1.63s;方法2:1.4s
4主题: 方法0:0.07s;方法1:0.16s;方法2:0.16s
2主题: 方法0:0.07s;方法1:0.10;方法2:0.09
1主题: 方法0:0.07s;方法1:0.08s;方法2:0.07s
我已经试过禁用优化,并使用不同的gcc版本没有区别
任何帮助表示赞赏。
编辑:在Linux中使用时钟返回'不正确的'时间,挂钟时间是我所需要的,所以使用ether omp_get_wtime()或Linux函数timeit会产生正确的结果。
您可以发布不同NUM_THREADS的时序结果? – CharlesB 2011-02-12 17:24:08
你在多核机器上运行吗?这个代码将受到CPU限制(与内存绑定或IO绑定相对),所以如果多线程能够在问题上抛出更多内核,它将只会改进。 – 2011-02-12 17:29:46
你没有长时间运行你的实验,所以有可能你的OMP时代实际上是由产生和杀死线程所支配的。尝试运行整个事情1000次,并计时整个事情。 – 2011-02-12 17:40:06