▶ 书上的计算圆周率的简单程序,主要是使用了自定义函数
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <openacc.h> #define N 100 #pragma acc routine seq
float ff(const float x)
{
return 4.0f / (1.0f + x * x);
} int main()
{
const float h = 1.0f / N;
float sumf = , result; #pragma acc parallel loop reduction(+:sumf)
for (int i = ; i < N; i++)
sumf += ff(h * (i - 0.5f)); result = h * sumf;
printf("\nN = %d, myPi = %f, diff = %e\n", N, result, result / 3.141592653589793238 - );
//getchar();
return ;
}
● 输出结果
D:\Code\OpenACC\OpenACCProject\OpenACCProject>pgcc main.c -acc -Minfo -o main_acc.exe
ff:
, Generating acc routine seq
Generating Tesla code
, FMA (fused multiply-add) instruction(s) generated
main:
, Accelerator kernel generated
Generating Tesla code
, #pragma acc loop gang, vector(100) /* blockIdx.x threadIdx.x */
Generating reduction(+:sumf)
, Generating implicit copy(sumf) D:\Code\OpenACC\OpenACCProject\OpenACCProject>main_acc.exe
launch CUDA kernel file=D:\Code\OpenACC\OpenACCProject\OpenACCProject\main.c function=main line= device= threadid= num_gangs= num_workers= vector_length= grid= block= shared memory=
launch CUDA kernel file=D:\Code\OpenACC\OpenACCProject\OpenACCProject\main.c function=main line= device= threadid= num_gangs= num_workers= vector_length= grid= block= shared memory= N = , myPi = 3.161500, diff = 6.336546e-03
PGI: "acc_shutdown" not detected, performance results might be incomplete.
Please add the call "acc_shutdown(acc_device_nvidia)" to the end of your application to ensure that the performance results are complete. Accelerator Kernel Timing data
D:\Code\OpenACC\OpenACCProject\OpenACCProject\main.c
main NVIDIA devicenum=
time(us):
: compute region reached time
: kernel launched time
grid: [] block: []
elapsed time(us): total= max= min= avg=
: reduction kernel launched time
grid: [] block: []
device time(us): total= max= min= avg=
: data region reached times
: data copyin transfers:
device time(us): total= max= min= avg=
: data copyout transfers:
device time(us): total= max= min= avg=