簡體   English   中英

使用openmp並行化C代碼

[英]parallelizing c code using openmp

我正在嘗試使用OpenMP並行執行以下程序:

#include <time.h>

// Program computes the total number of primes larger than 100000001 and smaller than 16000001.
main() {

int number = 100000001;
int primes[20];
int i, j, is_prime, index = 0, nprimes = 0;
time_t start_time, end_time;

start_time = time(NULL);
for (i = 0; i < 3000000; i++) {
    // get the next number to check if it is a prime
    number += 2;
    is_prime = 1;
    for (j = 2; j < 10001; j++) {
        if ((number % j) == 0) {
            is_prime = 0;
            break;
        }
    }
    // f0und a prime number. Count it and save the first 20 primes
    if (is_prime) nprimes++;
    if (is_prime && (index < 20)) {
        primes[index] = number;
        index++;
    }
}
for (i = 0; i < 20; i++)
    printf("%d is prime\n", primes[i]);
end_time = time(NULL);
printf("number of primes = %d, elapsed time is %d seconds\n", nprimes, end_time - start_time);
}

我所做的是這樣的:

#include <stdio.h>
#include <time.h>
#include <omp.h>
#define CHUNKSIZE 750000
//#define CHUNKSIZE2 2500

// Program computes the total number of primes larger than 100000001 and smaller than 16000001.
int main() {

int number = 100000001;
int primes[20];
int i, j, is_prime, index = 0, nprimes = 0;
time_t start_time, end_time;

start_time = time(NULL);
int chunk = CHUNKSIZE;
//int chunk2 = CHUNKSIZE2;
#pragma omp parallel shared(number, index, nprimes, chunk) private(i, j, is_prime)
{
#pragma omp parallel for schedule (dynamic, chunk)
for (i = 0; i < 3000000; i++) {
    // get the next number to check if it is a prime
    number += 2;
    is_prime = 1;
    //#pragma omp parallel for schedule (dynamic, chunk2)
    for (j = 2; j < 10001; j++) {
        if ((number % j) == 0) {
            is_prime = 0;
            break;
        }
    }
    // f0und a prime number. Count it and save the first 20 primes
    if (is_prime) nprimes++;
    if (is_prime && (index < 20)) {
        primes[index] = number;
        index++;
    }
}

 for (i = 0; i < 20; i++)
    printf("%d is prime\n", primes[i]);
    end_time = time(NULL);
    printf("number of primes = %d, elapsed time is %d seconds\n", nprimes, end_time - start_time);
 //return 0;
}

我嘗試了很多東西,但大多數都給了我更長或更長時間!

number變量在全局范圍內遞增,因此會形成障礙; 不能並行執行任何計算,每個線程必須等待前一個線程結束,以便number+=2部分保持一致。

您可以通過創建另一個基於線程的變量(此處為n )來避免這種情況,該變量的值基於循環索引( i

一個並行的編譯指示就足夠了:

#include <stdio.h>
#include <time.h>
#include <omp.h>
#define CHUNKSIZE 750000
//#define CHUNKSIZE2 2500

// Program computes the total number of primes larger than 100000001 and smaller than 16000001.
int main() {

int number = 100000001;
int n;
int primes[20];
int i, j, is_prime, index = 0, nprimes = 0;
time_t start_time, end_time;

start_time = time(NULL);
int chunk = CHUNKSIZE;
//int chunk2 = CHUNKSIZE2;

#pragma omp parallel for private(n, is_prime, j)
for (i = 0; i < 300000; i++) {
    // get the next number to check if it is a prime
    //number += 2;
    n = number + i*2;
    is_prime = 1;
    //#pragma omp parallel for schedule (dynamic, chunk2)
    for (j = 2; j < 10001; j++) {
        if ((n % j) == 0) {
            is_prime = 0;
            break;
        }
    }
    // f0und a prime number. Count it and save the first 20 primes
    if (is_prime) nprimes++;
    if (is_prime && (index < 20)) {
        primes[index] = n;
        index++;
    }
}

 for (i = 0; i < 20; i++)
    printf("%d is prime\n", primes[i]);
    end_time = time(NULL);
    printf("number of primes = %d, elapsed time is %d seconds\n", nprimes, end_time - start_time);
 //return 0;

}

gcc和精簡計算的結果,以避免等待過多:

$ gcc -fopenmp -o tt tt.c
$ time OMP_NUM_THREADS=1  ./tt
100000007 is prime
[...]
100000393 is prime
number of primes = 326390, elapsed time is 21 seconds

real    0m20.507s
user    0m20.492s
sys 0m0.001s
$ time OMP_NUM_THREADS=8  ./tt
101500027 is prime
[...]
105250049 is prime
number of primes = 325580, elapsed time is 3 seconds
real    0m3.041s
user    0m24.284s
sys 0m0.002s

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM