0
2

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 5 years have passed since last update.

PGIコンパイラを用いてOpenACCを利用

Last updated at Posted at 2018-01-16

PGIコンパイラを用いてOpenACCを利用してみる (#pragma acc kernels)。以下の例では、ライプニッツの公式を用いて円周率を求め、GCCとPGIコンパイラのそれぞれでかかった時間を書き添えた。

Leibniz_formula_for_pi.c
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

#define N 1600000000L

int main(int argc, char *argv[])
{
  double pi, piq = 1.0;
  long int li, upto = N;
  clock_t begin, end;

  if (argc > 1) upto = atol(argv[1]);

  begin = clock();

#pragma acc kernels
  for (li = 1; li < upto; li++) 
  {
    if (li % 2 == 0) piq += 1.0 / (2.0 * li + 1.0);
    else             piq -= 1.0 / (2.0 * li + 1.0);
  }

  pi = 4.0 * piq;

  end = clock();

  fprintf(stdout, "pi = %.12f (%.2f s)\n",
          pi, (double)(end - begin) / CLOCKS_PER_SEC);
  return EXIT_SUCCESS;
}
$ gcc -v 2>&1 | grep ^gcc
gcc version 5.4.0 20160609 (Ubuntu/IBM 5.4.0-6ubuntu1~16.04.5) 
$ gcc -o pi Leibniz_formula_for_pi.c
$ file pi
pi: ELF 64-bit LSB executable, 64-bit PowerPC or cisco 7500, version 1 (SYSV), dynamically linked, interpreter /lib64/ld64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=0133a85d075fb58a5c98988dffd16e3ec6b83a2a, not stripped
$ ./pi 3200000000
pi = 3.141592653276 (27.54 s)
$ which pgcc
/opt/pgi/linuxpower/17.10/bin/pgcc
$ pgcc --version | grep -v ^$
pgcc 17.10-0 linuxpower target on Linuxpower 
PGI Compilers and Tools
Copyright (c) 2017, NVIDIA CORPORATION.  All rights reserved.
$ pgcc -acc -o pi_acc Leibniz_formula_for_pi.c
$ file pi_acc
pi_acc: ELF 64-bit LSB executable, 64-bit PowerPC or cisco 7500, version 1 (SYSV), dynamically linked, interpreter /lib64/ld64.so.2, for GNU/Linux 3.2.0, not stripped
$ ./pi_acc 3200000000
pi = 3.141592653276 (14.05 s)
0
2
0

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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?