0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

OpenMPを用いて多重ループを高速化する場合

Posted at

目的

C言語でOpenMPを用いて、並列化を行い高速化を図る場合、思うように高速化がされない場合がある。その一つの例が多重ループを用いた時である。今回は多重ループを持ちいる場合のOpenMPを正しく使う方法を解説する。

TL;DR

多重ループをする場合は、ループの内側だけか両側に #pragma omp parallel forを置いてあげないといけない。

正解例 (両側配置)

openmp.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <omp.h>

double start,end;
int a[50][40], i, j;

int main(void){
start = omp_get_wtime();
  #pragma omp parallel for
  for(i = 0; i < 50; i++) {
    #pragma omp parallel for
    for(j = 0; j < 40; j++) {
      a[i][j] = i*j;
    }
  }
end = omp_get_wtime();

printf("Time:%e(sec)\n",end-start);


return 0;
}

(内側配置)

openmp.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <omp.h>

double start,end;
int a[50][40], i, j;

int main(void){
start = omp_get_wtime();
  for(i = 0; i < 50; i++) {
#pragma omp parallel for
    for(j = 0; j < 40; j++) {
      a[i][j] = i*j;
    }
  }
end = omp_get_wtime();

printf("Time:%e(sec)\n",end-start);


return 0;
}

計測結果

  • OpenMP無し
$ ./a.out
Time:2.463534e-05(sec)
  • OpenMP有
$ ./a.out
Time:1.131900e-03(sec)

間違い例

外側だけ配置していると上手く行かない

間違い.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <omp.h>

double start,end;
int a[50][40], i, j;

int main(void){
start = omp_get_wtime();
  #pragma omp parallel for
  for(i = 0; i < 50; i++) {
    for(j = 0; j < 40; j++) {
      a[i][j] = i*j;
    }
  }
end = omp_get_wtime();

printf("Time:%e(sec)\n",end-start);


return 0;
}
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?