OpenACC 简单的直方图
Posted cuancuancuanhao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OpenACC 简单的直方图相关的知识,希望对你有一定的参考价值。
? 简单的直方图,强调原子操作的使用
● 代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <openacc.h> 4 5 int main() 6 { 7 const int length = 1024, basket = 10; 8 int data[length], histgram[basket]; 9 10 srand(97); 11 for (int i = 0; i < basket; histgram[i++] = 0); 12 for (int i = 0; i < length; data[i++] = rand() % 10); 13 14 #pragma acc parallel loop 15 for (int i = 0; i < length; i++) 16 { 17 #pragma acc atomic update 18 histgram[data[i]]+=1; 19 } 20 21 for (int i = 0; i < basket; i++) 22 printf("histgram[%d] = %d ", i, histgram[i]); 23 24 getchar(); 25 return 0; 26 }
● 输出结果,在 Windows 里是错的,在 WSL 里是对的
D:CodeOpenACCOpenACCProjectOpenACCProject>pgcc -acc -Minfo main.c -o main_acc.exe main: 14, Accelerator kernel generated Generating Tesla code 15, #pragma acc loop gang, vector(128) /* blockIdx.x threadIdx.x */ 14, Generating implicit copyout(histgram[data]) Generating implicit copyin(data[:1024]) D:CodeOpenACCOpenACCProjectOpenACCProject>main_acc.exe launch CUDA kernel file=D:CodeOpenACCOpenACCProjectOpenACCProjectmain.c function=main line=14 device=0 threadid=1 num_gangs=8 num_workers=1 vector_length=128 grid=8 block=128 histgram[0] = 0 histgram[1] = 0 histgram[2] = 0 histgram[3] = 0 histgram[4] = 0 histgram[5] = 100 histgram[6] = 0 histgram[7] = 0 histgram[8] = 0 histgram[9] = 0
[email protected]:/mnt/d/Code/OpenACC/OpenACCProject/OpenACCProject$ pgcc -acc -Minfo main.c -o main_acc_ubuntu.exe main: 14, Accelerator kernel generated Generating Tesla code 15, #pragma acc loop gang, vector(128) /* blockIdx.x threadIdx.x */ 14, Generating implicit copyout(histgram[data]) Generating implicit copyin(data[:1024]) [email protected]:/mnt/d/Code/OpenACC/OpenACCProject/OpenACCProject$ ./main_acc_ubuntu.exe histgram[0] = 123 histgram[1] = 79 histgram[2] = 90 histgram[3] = 109 histgram[4] = 102 histgram[5] = 96 histgram[6] = 117 histgram[7] = 104 histgram[8] = 110 histgram[9] = 94
● 更新!!!把代码改了一下,在 Windos 下也正确了,还是简单语句的问题
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <openacc.h> 4 5 int main() 6 { 7 const int length = 1024, basket = 10; 8 int data[length], histgram[basket]; 9 10 srand(97); 11 for (int i = 0; i < basket; histgram[i++] = 0); 12 for (int i = 0; i < length; data[i++] = rand() % 10); 13 14 int temp; // 新定义一个变量 15 #pragma acc parallel loop 16 for (int i = 0; i < length; i++) 17 { 18 temp = data[i]; // 原子操作之前先取地址 19 #pragma acc atomic update 20 histgram[temp] += 1;// 使用 temp 作为下标 21 } 22 23 for (int i = 0; i < basket; i++) 24 printf("histgram[%d] = %d ", i, histgram[i]); 25 26 getchar(); 27 return 0; 28 }
以上是关于OpenACC 简单的直方图的主要内容,如果未能解决你的问题,请参考以下文章