1. #include <osg/Notify>
  2. #include <osgViewer/Viewer>
  3. #include <osgCompute/Memory>
  4. #include <osgCompute/Module>
  5. #include <osgCuda/Memory>
  6. #include <memory.h>
  7. #include <iostream>
  8. #pragma comment(lib, "osgViewerd.lib")
  9. #pragma comment(lib, "osgComputed.lib")
  10. #pragma comment(lib, "osgCudad.lib")
  11. #pragma comment(lib, "osgd.lib")
  12. extern "C"
  13. void MyCudaTest(unsigned int numBlocks, unsigned int numThreads, void * bytes1, void * bytes2, void * bytes3);
  14. class MyModule : public osgCompute::Module
  15. {
  16. public:
  17. MyModule()
  18. :osgCompute::Module()
  19. {
  20. clearLocal();
  21. }
  22. META_Object(osgCompute,MyModule)
  23. virtual bool init()
  24. {
  25. _numThreads = ;
  26. _numBlocks = _buffer1->getDimension()/_numThreads;
  27. return osgCompute::Module::init();
  28. }
  29.  
  30. virtual void clear()
  31. {
  32. clearLocal();
  33. osgCompute::Module::clear();
  34. }
  35.  
  36. virtual void launch()
  37. {
  38. MyCudaTest(_numBlocks, _numThreads, _buffer1->map(), _buffer2->map(), _buffer3->map());
  39. }
  40.  
  41. inline void setBuffer1(osgCompute::Memory * buffer)
  42. {
  43. _buffer1 = buffer;
  44. }
  45. inline void setBuffer2(osgCompute::Memory * buffer)
  46. {
  47. _buffer2 = buffer;
  48. }
  49. inline void setBuffer3(osgCompute::Memory * buffer)
  50. {
  51. _buffer3 = buffer;
  52. }
  53. protected:
  54. virtual ~MyModule()
  55. {
  56. clearLocal();
  57. }
  58.  
  59. virtual void clearLocal()
  60. {
  61. _buffer1 = NULL;
  62. _buffer2 = NULL;
  63. _buffer3 = NULL;
  64. }
  65.  
  66. protected:
  67. unsigned int _numThreads;
  68. unsigned int _numBlocks;
  69. osg::ref_ptr<osgCompute::Memory> _buffer1;
  70. osg::ref_ptr<osgCompute::Memory> _buffer2;
  71. osg::ref_ptr<osgCompute::Memory> _buffer3;
  72. private:
  73. MyModule(const MyModule &, const osg::CopyOp &)
  74. {
  75.  
  76. }
  77. inline MyModule & operator = (const MyModule &)
  78. {
  79. return *this;
  80. }
  81. };
  82.  
  83. int main()
  84. {
  85. unsigned int a[] = {, , };
  86. unsigned int b[] = {, , };
  87. unsigned int c[] = {, , };
  88.  
  89. unsigned int num = sizeof(a)/sizeof(unsigned int);
  90.  
  91. osg::ref_ptr<osgCuda::Memory> buffer1 = new osgCuda::Memory;
  92. buffer1->setElementSize(sizeof(int));
  93. buffer1->setDimension(, num);
  94. buffer1->init();
  95.  
  96. osg::ref_ptr<osgCuda::Memory> buffer2 = new osgCuda::Memory;
  97. buffer2->setElementSize(sizeof(int));
  98. buffer2->setDimension(, num);
  99. buffer2->init();
  100.  
  101. osg::ref_ptr<osgCuda::Memory> buffer3 = new osgCuda::Memory;
  102. buffer3->setElementSize(sizeof(int));
  103. buffer3->setDimension(, num);
  104. buffer3->init();
  105.  
  106. osg::ref_ptr<MyModule> module = new MyModule;
  107. module->setBuffer1(buffer1.get());
  108. module->setBuffer2(buffer2.get());
  109. module->setBuffer3(buffer3.get());
  110. module->init();
  111.  
  112. unsigned int * bufferPtr1 = (unsigned int *)(buffer1->map(osgCompute::MAP_HOST_TARGET));
  113. memcpy(bufferPtr1, a, sizeof(a));
  114.  
  115. unsigned int * bufferPtr2 = (unsigned int *)(buffer2->map(osgCompute::MAP_HOST_TARGET));
  116. memcpy(bufferPtr2, b, sizeof(b));
  117.  
  118. unsigned int * bufferPtr3 = (unsigned int *)(buffer3->map(osgCompute::MAP_HOST_TARGET));
  119. memcpy(bufferPtr3, c, sizeof(c));
  120. module->launch();
  121.  
  122. bufferPtr3 = (unsigned int *)(buffer3->map(osgCompute::MAP_HOST_SOURCE));
  123. std::cout << "并行计算:(1, 2, 3)于(4, 5, 6)的和:"<< std::endl;
  124. for (unsigned int i = ; i < buffer3->getDimension(); ++i)
  125. {
  126. std::cout << bufferPtr3[i] << std::endl;
  127. }
  128.  
  129. }
  1. #include <cuda_runtime.h>
  2. __global__ void addKernel(int * bytes1, int * bytes2, int * bytes3)
  3. {
  4. int tid = blockIdx.x;
  5. bytes3[tid] = bytes2[tid] + bytes1[tid];
  6. }
  7. extern "C"
  8. void MyCudaTest(unsigned int numBlocks, unsigned int numThreads, void * bytes1, void * bytes2, void * bytes3)
  9. {
  10. addKernel<<<numBlocks, numThreads>>>((int *)bytes1, (int *)bytes2, (int *)bytes3);
  11. }

osg + cuda的更多相关文章

  1. 朱丽叶—Cuda+OSG

    #include <cuda_runtime.h> #include <osg/Image> ; typedef struct cuComplex { float r; flo ...

  2. CUDA[2] Hello,World

    Section 0:Hello,World 这次我们亲自尝试一下如何用粗(CU)大(DA)写程序 CUDA最新版本是7.5,然而即使是最新版本也不兼容VS2015 ...推荐使用VS2012 进入VS ...

  3. CUDA[1] Introductory

    Section 0 :Induction of CUDA CUDA是啥?CUDA®: A General-Purpose Parallel Computing Platform and Program ...

  4. Couldn't open CUDA library cublas64_80.dll etc. tensorflow-gpu on windows

    I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\stream_executor\dso_load ...

  5. OSG计时器与时间戳

    static osg::Timer* sendMsgTimer = new osg::Timer; if (sendMsgTimer->time_m()>100)//100ms {// d ...

  6. ubuntu 16.04 + N驱动安装 +CUDA+Qt5 + opencv

    Nvidia driver installation(after download XX.run installation file) 1. ctrl+Alt+F1   //go to virtual ...

  7. OSG消息机制之消息分析

    OSG消息接收在头文件有各种事件的相关参数

  8. OSG消息机制之事件处理概述

    OSG的消息机制包括好多个头文件预定义及多个类. 首先,消息接收相关的类当属osgGA::GUIEventHandler和osgGA::GUIEventAdapter这两个类了.前者处理OSG程序与用 ...

  9. OSG 3D场景渲染编程概述

    OSG是Open Scene Graphic的缩写,是基于C++平台的使用OpenGL技术的开源3D场景开发. vs环境安装或者是在Ubuntu中环境的安装网上教程很多,都是大同小异的,认真操作容易成 ...

随机推荐

  1. 给Unity3d添加一个漂亮的标题栏

    我们在做好一个小Unity3d APP,我们一般都会兴致勃勃的导出一个exe,尝试着玩我们的app.感觉还不错,有板有眼的了.然而事与愿违,我们APP里面的内容挺漂亮的,但是它的标题栏是windows ...

  2. Unity3d请求webservice

    我们在对接第三方sdk时,第三方sdk通常会以一个webservice接口的形式供我们来调用.而这些接口会以提供我们get,post,soap等协议来进行访问.get,post方法相信大家都比较熟悉了 ...

  3. shrio配置说明

    1.引入Shiro的Maven依赖 <!-- Spring 整合Shiro需要的依赖 --> <dependency> <groupId>org.apache.sh ...

  4. Web网页数据抓取(C/S)

    通过程序自动的读取其它网站网页显示的信息,类似于爬虫程序.比方说我们有一个系统,要提取BaiDu网站上歌曲搜索排名.分析系统在根据得到的数据进行数据分析.为业务提供参考数据. 为了完成以上的需求,我们 ...

  5. 【IIS】windows2008 ii7 设置访问网站提示帐号密码登录

    3个步骤: 1.添加windows身份验证: windows2008默认是不启用的,需要我们自己去启动,在管理工具 - 服务器管理- 角色 ,拉下去,下面有个[添加角色服务],安全性- Windows ...

  6. apache bench(ab)压力测试模拟POSt请求

    ab命令格式: -N|--count 总请求数,缺省 : 5w -C|--clients 并发数, 缺省 : 100 -R|--rounds 测试次数, 缺省 : 10 次 -S|-sleeptime ...

  7. Tiny6410之控制icache驱动

    什么是cache:    基于程序访问的局限性,在主存和CPU通用寄存器之间设置了一类高速的.容量较小的存储器,把正在执行的指令地址附件的一部分指令或数据从主存调入这类存储器,供CPU 在一段时间内使 ...

  8. ResScope (软件资源分析)V1.94 绿色版

    软件名称:ResScope (软件资源分析)V1.94 绿色版软件类别:国产软件运行环境:Windows软件语言:简体中文授权方式:免费版软件大小:1.47 MB软件等级:整理时间:2015-01-0 ...

  9. moodle笔记之-权限api

    <?php//权限定义$capabilities = array( 'mod/mytest:managefiles' => array(//具体的权限:插件类型/插件名/权限 这里是增加一 ...

  10. 移动HTML 5前端性能优化指南(转载)

    前端工程师的菜!最近移动Html 5越来越火,想有一个体验流畅的Html 5 应用,这篇优化指南就别放过咯.腾讯的同学将关键的注意点与优化方法都总结出来,全文高能干货,非常值得深度学习 >> ...