▶ 书中第4章,数据管理部分的代码和说明

● 代码,关于 copy,copyin,copyout,create

  1. #include <stdio.h>
  2. #include <openacc.h>
  3.  
  4. int main()
  5. {
  6. const int length = ;
  7. int a[length], b[length], c[length],d[length];
  8.  
  9. for (int i = ; i < length; a[i] = b[i] = c[i] = );
  10. {
  11. #pragma acc kernels create(d)
  12. for (int i = ; i < length; i++)
  13. {
  14. a[i] ++;
  15. c[i] = a[i] + b[i];
  16. d[i] = ;
  17. }
  18. }
  19. for (int i = ; i < ; i++)
  20. printf("a[%d] = %d, c[%d] = %d\n", i, a[i], i, c[i]);
  21. getchar();
  22. return ;
  23. }

● 输出结果,显式创建了中间变量 d,隐式创建了 a,b,c,并具有不同的拷贝属性

  1. D:\Code\OpenACC\OpenACCProject\OpenACCProject>pgcc -acc -Minfo main.c -o main_acc.exe
  2. main:
  3. , Generating create(d[:])
  4. Generating implicit copyout(c[:])
  5. Generating implicit copyin(b[:])
  6. Generating implicit copy(a[:])
  7. , Loop is parallelizable
  8. Accelerator kernel generated
  9. Generating Tesla code
  10. , #pragma acc loop gang, vector(128) /* blockIdx.x threadIdx.x */

● 在 kernels 里单独使用 copyout 时报警告:PGC-W-0996-The directive #pragma acc copyout is deprecated; use #pragma acc declare copyout instead (main.c: XX)

● enter data 和 exit data 用于 C++。

■ 首先,windows 中 pgi 不支持 C++ 编译,只有 pgcc.exe 而没有 pgc++*.exe,只能乖乖到 Linux 下去写!

■ 书上的代码有点问题,大意是 OpenACC 的 copy 是浅拷贝,对于内含指针的数据结构(如 vector,class)不会连着指针指向的对象一起拷。这里有两种解决办法,一种是去结构化,将 class 中的数据集中成简单数组来进行拷贝;另一种是使用 Managed 内存,也就不存在显式拷贝的问题了。【https://stackoverflow.com/questions/53860467/how-to-copy-on-gpu-a-vector-of-vector-pointer-memory-allocated-in-openacc】

■ 书上的代码没有采用这两种解决方案,会报错 “call to cuStreamSynchronize returned error 700: Illegal address during kernel execution” 以及 “call to cuStreamSynchronize returned error 700: Illegal address during kernel execution”,这个问题还蛮常见的【https://stackoverflow.com/search?q=call+to+returned+error+700%3A+Illegal+address+during+kernel+execution】

● 使用去结构化来使用数组

  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdint>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. const int vectorCount = , vectorLength = ;
  10. long sum = ;
  11.  
  12. vector<int32_t> *vectorTable = new vector<int32_t>[vectorCount]; // 1024 个向量,每个向量放入 20 个元素
  13. for (int i = ; i < vectorCount; i++)
  14. {
  15. for (int j = ; j < vectorLength; j++)
  16. vectorTable[i].push_back(i);
  17. }
  18. int32_t **arrayTable = new int32_t *[vectorCount]; // 仅包含向量数据的数组,与 vectorTable 对应
  19. int *vectorSize = new int[vectorCount]; // 每个向量的尺寸
  20.  
  21. #pragma acc enter data create(arrayTable [0:vectorCount] [0:0]) // 设备中创建 arryTable,注意维度
  22. for (int i = ; i < vectorCount; i++)
  23. {
  24. int sze = vectorTable[i].size();
  25. vectorSize[i] = sze;
  26. arrayTable[i] = vectorTable[i].data(); // 把每个向量数据的指针赋给 arrayTable
  27. #pragma acc enter data copyin(arrayTable [i:1][:sze]) // 把每个向量的数据拷贝进设备
  28. }
  29. #pragma acc enter data copyin(vectorSize[:vectorCount]) // 向量尺寸也放进设备
  30.  
  31. #pragma acc parallel loop gang vector reduction(+: sum) present(arrayTable, vectorSize) // 规约计算
  32. for (int i = ; i < vectorCount; i++)
  33. {
  34. for (int j = ; j < vectorSize[i]; ++j)
  35. sum += arrayTable[i][j];
  36. }
  37. cout << "Sum: " << sum << endl;
  38.  
  39. #pragma acc exit data delete (vectorSize)
  40. #pragma acc exit data delete (arrayTable)
  41. delete[] vectorSize;
  42. delete[] vectorTable;
  43. return ;
  44. }

● 输出结果

  1. cuan@CUAN:~$ pgc++ main.cpp -o main.exe --c++ -ta=tesla -Minfo -acc
  2. main:
  3. , Generating enter data create(arrayTable[:][:])
  4. , Generating enter data copyin(arrayTable[i][:sze+],vectorSize[:])
  5. Generating implicit copy(sum)
  6. Generating present(vectorSize[:])
  7. Generating Tesla code
  8. , Generating reduction(+:sum)
  9. , #pragma acc loop gang, vector(128) /* blockIdx.x threadIdx.x */
  10. , #pragma acc loop seq
  11. , Generating present(arrayTable[:][:])
  12. , Loop is parallelizable
  13. , Generating exit data delete(vectorSize[:],arrayTable[:][:])
  14. cuan@CUAN:~$ ./main.exe
  15. Sum:

● 使用 Managed 内存

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class ivector
  6. {
  7. public:
  8. int len;
  9. int *arr;
  10. ivector(int length)
  11. {
  12. len = length;
  13. arr = new int[len];
  14. #pragma acc enter data copyin(this)
  15. #pragma acc enter data create(arr [0:len])
  16. #pragma acc parallel loop present(arr [0:len])
  17. for (int iend = len, i = ; i < iend; i++) // 使用临时变量 iend,防止编译器认为 len 值在循环中会改变,从而拒绝并行化
  18. arr[i] = i;
  19. }
  20.  
  21. ivector(const ivector &s)
  22. {
  23. len = s.len;
  24. arr = new int[len];
  25. #pragma acc enter data copyin(this)
  26. #pragma acc enter data create(arr [0:len])
  27. #pragma acc parallel loop present(arr [0:len], s.arr [0:len]) // s 也已经在设备上了
  28. for (int iend = len, i = ; i < iend; i++)
  29. arr[i] = s.arr[i];
  30. }
  31.  
  32. ~ivector()
  33. {
  34. #pragma acc exit data delete (arr) // 销毁对象时依次销毁设备上的 arr 和 this
  35. #pragma acc exit data delete (this)
  36. cout << "deconstruction!" << endl;
  37. delete[] arr;
  38. len = ;
  39. }
  40.  
  41. int &operator[](int i)
  42. {
  43. if (i < || i >= this->len)
  44. return arr[];
  45. return arr[i];
  46. }
  47.  
  48. void add(int c)
  49. {
  50. #pragma acc kernels loop present(arr [0:len]) // 每次涉及修改 arr 的操作都要注明 present
  51. for (int iend = len, i = ; i < iend; i++)
  52. arr[i] += c;
  53. }
  54.  
  55. void updateHost() // 手动更新主机端数据
  56. {
  57. #pragma acc update host(arr [0:len])
  58. }
  59. };
  60.  
  61. int main()
  62. {
  63. ivector s1();
  64. s1.add();
  65. s1.updateHost();
  66. cout << "s1[1] = " << s1[] << endl;
  67.  
  68. ivector s2(s1);
  69. s2.updateHost();
  70. cout << "s2[1] = " << s2[] << endl;
  71.  
  72. return ;
  73. }

● 输出结果,不加 -ta=tesla:managed 会报错【填坑】

  1. cuan@CUAN:~$ pgc++ main.cpp -o main.exe --c++ -ta=tesla:managed -Minfo -acc
  2. ivector::ivector(int):
  3. , Generating enter data copyin(this[:])
  4. Generating enter data create(arr[:len])
  5. Generating Tesla code
  6. , #pragma acc loop gang, vector(128) /* blockIdx.x threadIdx.x */
  7. , Generating implicit copy(this[:])
  8. Generating present(arr[:len])
  9. ivector::ivector(const ivector&):
  10. , Generating enter data create(arr[:len])
  11. Generating enter data copyin(this[:])
  12. Generating present(arr[:len])
  13. Generating Tesla code
  14. , #pragma acc loop gang, vector(128) /* blockIdx.x threadIdx.x */
  15. , Generating implicit copyin(s[:])
  16. Generating implicit copy(this[:])
  17. Generating present(s->arr[:len])
  18. ivector::~ivector():
  19. , Generating exit data delete(this[:],arr[:])
  20. ivector::add(int):
  21. , Generating Tesla code
  22. , Accelerator serial kernel generated
  23. Generating implicit copy(this[:])
  24. Generating present(arr[:len])
  25. , Loop is parallelizable
  26. Generating Tesla code
  27. , #pragma acc loop gang, vector(128) /* blockIdx.x threadIdx.x */
  28. ivector::updateHost():
  29. , Generating update self(arr[:len])
  30. cuan@CUAN:~$ ./main.exe
  31. launch CUDA kernel file=/home/cuan/main.cpp function=_ZN7ivectorC1Ei line= device= threadid= num_gangs= num_workers= vector_length= grid= block=
  32. launch CUDA kernel file=/home/cuan/main.cpp function=_ZN7ivector3addEi line= device= threadid= num_gangs= num_workers= vector_length= grid= block=
  33. launch CUDA kernel file=/home/cuan/main.cpp function=_ZN7ivector3addEi line= device= threadid= num_gangs= num_workers= vector_length= grid= block=
  34. s1[] =
  35. launch CUDA kernel file=/home/cuan/main.cpp function=_ZN7ivectorC1ERKS_ line= device= threadid= num_gangs= num_workers= vector_length= grid= block=
  36. s2[] =
  37. deconstruction!
  38. deconstruction!

● 在这本书上找到了 C++ 中使用 OpenACC 的办法 【https://www.elsevier.com/books/parallel-programming-with-openacc/farber/978-0-12-410397-9】,代码是【https://github.com/rmfarber/ParallelProgrammingWithOpenACC/tree/master/Chapter05】下的 accList.double.cpp

  1. // accList.h
  2. #ifndef ACC_LIST_H
  3. #define ACC_LIST_H
  4.  
  5. #include <cstdlib>
  6. #include <cassert>
  7. #ifdef _OPENACC
  8. #include <openacc.h>
  9. #endif
  10.  
  11. template<typename T>
  12. class accList
  13. {
  14. public:
  15. explicit accList() {}
  16. explicit accList(size_t size) // 构造函数把 this 指针拷进设备,然后创建内存
  17. {
  18. #pragma acc enter data copyin(this)
  19. allocate(size);
  20. }
  21.  
  22. ~accList() // 析构时释放内存,再删除 this 指针
  23. {
  24. release();
  25. #pragma acc exit data delete(this)
  26. }
  27.  
  28. #pragma acc routine seq
  29. T& operator[](size_t idx)
  30. {
  31. return _A[idx];
  32. }
  33.  
  34. #pragma acc routine seq
  35. const T& operator[](size_t idx) const
  36. {
  37. return _A[idx];
  38. }
  39.  
  40. size_t size() const
  41. {
  42. return _size;
  43. }
  44.  
  45. accList& operator=(const accList& B)
  46. {
  47. allocate(B.size());
  48. for (size_t j = ; j < _size; ++j)
  49. {
  50. _A[j] = B[j];
  51. }
  52. accUpdateDevice();
  53. return *this;
  54. }
  55.  
  56. void insert(size_t idx, const T& val)
  57. {
  58. _A[idx] = val;
  59. }
  60. void insert(size_t idx, const T* val)
  61. {
  62. _A[idx] = *val;
  63. }
  64.  
  65. void accUpdateSelf()
  66. {
  67. accUpdateSelfT(_A, );
  68. }
  69. void accUpdateDevice()
  70. {
  71. accUpdateDeviceT(_A, );
  72. }
  73.  
  74. private:
  75. T * _A{ nullptr }; // 数据成员只有指针和长度
  76. size_t _size{ };
  77.  
  78. void release()
  79. {
  80. if (_size > )
  81. {
  82. #pragma acc exit data delete(_A[0:_size]) // 释放内存时删除设备内存
  83. delete[] _A;
  84. _A = nullptr;
  85. _size = ;
  86. }
  87. }
  88.  
  89. void allocate(size_t size)
  90. {
  91. if (_size != size) // 申请内存尺寸与当前尺寸不一致时重新开辟一块
  92. {
  93. release();
  94. _size = size;
  95. #pragma acc update device(_size)
  96. if (_size > )
  97. {
  98. _A = new T[_size];
  99. #ifdef _OPENACC // 有 OpenACC 的话检查 _A 是否已经在设备上了
  100. assert(!acc_is_present(&_A[], sizeof(T)));
  101. #endif
  102. #pragma acc enter data create(_A[0:_size]) // 在设备上申请新内存
  103. }
  104. }
  105. }
  106.  
  107. template<typename U>
  108. void accUpdateSelfT(U *p, long)
  109. {
  110. #pragma acc update self(p[0:_size])
  111. }
  112.  
  113. template<typename U>
  114. auto accUpdateSelfT(U *p, int) -> decltype(p->accUpdateSelf())
  115. {
  116. for (size_t j = ; j < _size; ++j)
  117. {
  118. p[j].accUpdateSelf();
  119. }
  120. }
  121.  
  122. template<typename U>
  123. void accUpdateDeviceT(U *p, long)
  124. {
  125. #pragma acc update device(p[0:_size])
  126. }
  127.  
  128. template<typename U>
  129. auto accUpdateDeviceT(U *p, int) -> decltype(p->accUpdateDevice())
  130. {
  131. for (size_t j = ; j < _size; ++j)
  132. {
  133. p[j].accUpdateDevice();
  134. }
  135. }
  136. };
  137. #endif
  138.  
  139. // main.cpp
  140. #include <iostream>
  141. #include <cstdlib>
  142. #include <cstdint>
  143. #include "accList.h"
  144. using namespace std;
  145. #ifndef N
  146. #define N 1024
  147. #endif
  148.  
  149. int main()
  150. {
  151. accList<double> A(N), B(N);
  152. for (int i = ; i < B.size(); ++i)
  153. B[i] = 2.5;
  154. B.accUpdateDevice(); // 手动更新设备内存
  155. #pragma acc parallel loop gang vector present(A,B)
  156. for (int i = ; i < A.size(); ++i)
  157. A[i] = B[i] + i;
  158. A.accUpdateSelf(); // 手动更新主机内存
  159. for (int i = ; i<; ++i)
  160. cout << "A[" << i << "]: " << A[i] << endl;
  161. cout << "......" << endl;
  162. for (int i = N - ; i<N; ++i)
  163. cout << "A[" << i << "]: " << A[i] << endl;
  164. return ;
  165. }

● 运行结果

  1. cuan@CUAN:~/acc$ pgc++ main.cpp -o main.exe -Minfo -acc
  2. main:
  3. , Generating present(B,A)
  4. Generating Tesla code
  5. , #pragma acc loop gang, vector(128) /* blockIdx.x threadIdx.x */
  6. accList<double>::accList(unsigned long):
  7. , include "accList.h"
  8. , Generating enter data copyin(this[:])
  9. accList<double>::~accList():
  10. , include "accList.h"
  11. , Generating exit data delete(this[:])
  12. accList<double>::operator [](unsigned long):
  13. , include "accList.h"
  14. , Generating acc routine seq
  15. Generating Tesla code
  16. accList<double>::size() const:
  17. , include "accList.h"
  18. , Generating implicit acc routine seq
  19. Generating acc routine seq
  20. Generating Tesla code
  21. accList<double>::release():
  22. , include "accList.h"
  23. , Generating exit data delete(_A[:_size])
  24. accList<double>::allocate(unsigned long):
  25. , include "accList.h"
  26. , Generating update device(_size)
  27. , Generating enter data create(_A[:_size])
  28. void accList<double>::accUpdateSelfT<double>(T1 *, long):
  29. , include "accList.h"
  30. , Generating update self(p[:_size])
  31. void accList<double>::accUpdateDeviceT<double>(T1 *, long):
  32. , include "accList.h"
  33. , Generating update device(p[:_size])
  34. cuan@CUAN:~/acc$ ./main.exe
  35. launch CUDA kernel file=/home/cuan/acc/main.cpp function=main line= device= threadid= num_gangs= num_workers= vector_length= grid= block=
  36. A[]: 2.5
  37. A[]: 3.5
  38. A[]: 4.5
  39. A[]: 5.5
  40. A[]: 6.5
  41. A[]: 7.5
  42. A[]: 8.5
  43. A[]: 9.5
  44. A[]: 10.5
  45. A[]: 11.5
  46. ......
  47. A[]: 1016.5
  48. A[]: 1017.5
  49. A[]: 1018.5
  50. A[]: 1019.5
  51. A[]: 1020.5
  52. A[]: 1021.5
  53. A[]: 1022.5
  54. A[]: 1023.5
  55. A[]: 1024.5
  56. A[]: 1025.5
  57.  
  58. Accelerator Kernel Timing data
  59. /home/cuan/acc/main.cpp
  60. main NVIDIA devicenum=
  61. time(us):
  62. : compute region reached time
  63. : kernel launched time
  64. grid: [] block: []
  65. device time(us): total= max= min= avg=
  66. elapsed time(us): total= max= min= avg=
  67. : data region reached times
  68. /home/cuan/acc/main.cpp
  69. _ZN7accListIdEC1Em NVIDIA devicenum=
  70. time(us):
  71. : data region reached times
  72. : data copyin transfers:
  73. device time(us): total= max= min= avg=
  74. /home/cuan/acc/main.cpp
  75. _ZN7accListIdED1Ev NVIDIA devicenum=
  76. time(us):
  77. : data region reached times
  78. /home/cuan/acc/main.cpp
  79. _ZN7accListIdE7releaseEv NVIDIA devicenum=
  80. time(us):
  81. : data region reached times
  82. : data copyin transfers:
  83. device time(us): total= max= min= avg=
  84. /home/cuan/acc/main.cpp
  85. _ZN7accListIdE8allocateEm NVIDIA devicenum=
  86. time(us):
  87. : update directive reached times
  88. : data copyin transfers:
  89. device time(us): total= max= min= avg=
  90. : data region reached times
  91. : data copyin transfers:
  92. device time(us): total= max= min= avg=
  93. /home/cuan/acc/main.cpp
  94. _ZN7accListIdE14accUpdateSelfTIdEEvPT_l NVIDIA devicenum=
  95. time(us):
  96. : update directive reached time
  97. : data copyout transfers:
  98. device time(us): total= max= min= avg=
  99. /home/cuan/acc/main.cpp
  100. _ZN7accListIdE16accUpdateDeviceTIdEEvPT_l NVIDIA devicenum=
  101. time(us):
  102. : update directive reached time
  103. : data copyin transfers:
  104. device time(us): total= max= min= avg=

OpenACC数据管理语句的更多相关文章

  1. sql数据管理语句

    一.数据管理 1.增加数据 INSERT INTO student VALUES(1,'张三','男',20); -- 插入所有字段.一定依次按顺序插入 -- 注意不能少或多字段值 如只需要插入部分字 ...

  2. 基于指令的移植方式的几个重要概念的理解(OpenHMPP, OpenACC)-转载

    引言: 什么是基于指令的移植方式呢?首先我这里说的移植可以理解为把原先在CPU上跑的程序放到像GPU一样的协处理器上跑的这个过程.在英文里可以叫Porting.移植有两种方式:一种是使用CUDA或者O ...

  3. 【并行计算-CUDA开发】OpenACC与OpenHMPP

    在西雅图超级计算大会(SC11)上发布了新的基于指令的加速器并行编程标准,既OpenACC.这个开发标准的目的是让更多的编程人员可以用到GPU计算,同时计算结果可以跨加速器使用,甚至能用在多核CPU上 ...

  4. python第六天 函数 python标准库实例大全

    今天学习第一模块的最后一课课程--函数: python的第一个函数: 1 def func1(): 2 print('第一个函数') 3 return 0 4 func1() 1 同时返回多种类型时, ...

  5. whdxlib

    1 数据库系统实现 实 验 指 导 书 齐心 彭彬 计算机工程与软件实验中心 2016 年 3 月2目 录实验一.JDBC 应用程序设计(2 学时) ......................... ...

  6. oracle DML(数据管理语言)sql 基本语句

  7. Hadoop的数据管理

    Hadoop的数据管理,主要包括Hadoop的分布式文件系统HDFS.分布式数据库HBase和数据仓库工具Hive的数据管理. 1.HDFS的数据管理 HDFS是分布式计算的存储基石,Hadoop分布 ...

  8. R语言实战(二)数据管理

    本文对应<R语言实战>第4章:基本数据管理:第5章:高级数据管理 创建新变量 #建议采用transform()函数 mydata <- transform(mydata, sumx ...

  9. 夜黑风高的夜晚用SQL语句做了一些想做的事·······

         IT这条漫漫长路注定是孤独的,陪伴我们的只有那些不知冷暖的代码语句和被手指敲打的磨掉了键上的标识的键盘. 之所以可以继续坚持下去,是因为心中有一份永不熄灭的激情. 成功的路上让我们为自己带盐 ...

随机推荐

  1. HTTP、TCP、UDP以及SOCKET

    HTTP.TCP.UDP以及SOCKET 一.TCP/IP代表传输控制协议/网际协议,指的是一系列协组. 可分为四个层次:数据链路层.网络层.传输层和应用层. 在网络层:有IP协议.ICMP协议.AR ...

  2. php取浮点数后两位的方法

    $num = 10.4567; //第一种:利用round()对浮点数进行四舍五入echo round($num,2); //10.46 //第二种:利用sprintf格式化字符串$format_nu ...

  3. stenciljs 学习三 组件生命周期

    stenciljs 组件包含好多生命周期方法, will did load update unload 实现生命周期的方法比价简单类似 componentWillLoad ....,使用typescr ...

  4. 无法对 数据库'XXXXX' 执行 删除,因为它正用于复制

    无法对 数据库'XXXXX' 执行 删除,因为它正用于复制. (.Net SqlClient Data Provider) 使用以下方式一般可以解决 sp_removedbreplication 'X ...

  5. EXCEL函数LookUp, VLOOKUP,HLOOKUP应用详解(含中文参数解释)

    关于VLOOKUP函数的用法 “Lookup”的汉语意思是“查找”,在Excel中与“Lookup”相关的函数有三个:VLOOKUP.HLOOKUO和LOOKUP.下面介绍VLOOKUP函数的用法. ...

  6. PHP开源的项目管理软件

    禅道 http://devel.zentao.net/help-book-zentaophphelp.html PHP session详讲 http://blog.163.com/lgh_2002/b ...

  7. 【转】每天一个linux命令(50):crontab命令

    原文网址:http://www.cnblogs.com/peida/archive/2013/01/08/2850483.html 前一天学习了 at 命令是针对仅运行一次的任务,循环运行的例行性计划 ...

  8. JSOI2008——星球大战

    题目:https://www.luogu.org/problemnew/show/1197 并查集. 难点是若依次去掉点在求连通块个数,时间太长. 精妙的思维:先全部读入,再逆向求连通块个数——增加点 ...

  9. 【jmeter】jmeter环境搭建

    一. 工具描述 apache jmeter是100%的java桌面应用程序,它被设计用来加载被测试软件功能特性.度量被测试软件的性能.设计jmeter的初衷是测试web应用,后来又扩充了其它的功能.j ...

  10. ASM配置管理

    http://blog.chinaunix.net/uid-22646981-id-3060280.htmlhttp://blog.sina.com.cn/s/blog_6a5aa0300102uys ...