#include <osg/Notify>
#include <osgViewer/Viewer>
#include <osgCompute/Memory>
#include <osgCompute/Module>
#include <osgCuda/Memory>
#include <memory.h>
#include <iostream>
#pragma comment(lib, "osgViewerd.lib")
#pragma comment(lib, "osgComputed.lib")
#pragma comment(lib, "osgCudad.lib")
#pragma comment(lib, "osgd.lib")
extern "C"
void MyCudaTest(unsigned int numBlocks, unsigned int numThreads, void * bytes1, void * bytes2, void * bytes3);
class MyModule : public osgCompute::Module
{
public:
MyModule()
:osgCompute::Module()
{
clearLocal();
}
META_Object(osgCompute,MyModule)
virtual bool init()
{
_numThreads = ;
_numBlocks = _buffer1->getDimension()/_numThreads;
return osgCompute::Module::init();
} virtual void clear()
{
clearLocal();
osgCompute::Module::clear();
} virtual void launch()
{
MyCudaTest(_numBlocks, _numThreads, _buffer1->map(), _buffer2->map(), _buffer3->map());
} inline void setBuffer1(osgCompute::Memory * buffer)
{
_buffer1 = buffer;
}
inline void setBuffer2(osgCompute::Memory * buffer)
{
_buffer2 = buffer;
}
inline void setBuffer3(osgCompute::Memory * buffer)
{
_buffer3 = buffer;
}
protected:
virtual ~MyModule()
{
clearLocal();
} virtual void clearLocal()
{
_buffer1 = NULL;
_buffer2 = NULL;
_buffer3 = NULL;
} protected:
unsigned int _numThreads;
unsigned int _numBlocks;
osg::ref_ptr<osgCompute::Memory> _buffer1;
osg::ref_ptr<osgCompute::Memory> _buffer2;
osg::ref_ptr<osgCompute::Memory> _buffer3;
private:
MyModule(const MyModule &, const osg::CopyOp &)
{ }
inline MyModule & operator = (const MyModule &)
{
return *this;
}
}; int main()
{
unsigned int a[] = {, , };
unsigned int b[] = {, , };
unsigned int c[] = {, , }; unsigned int num = sizeof(a)/sizeof(unsigned int); osg::ref_ptr<osgCuda::Memory> buffer1 = new osgCuda::Memory;
buffer1->setElementSize(sizeof(int));
buffer1->setDimension(, num);
buffer1->init(); osg::ref_ptr<osgCuda::Memory> buffer2 = new osgCuda::Memory;
buffer2->setElementSize(sizeof(int));
buffer2->setDimension(, num);
buffer2->init(); osg::ref_ptr<osgCuda::Memory> buffer3 = new osgCuda::Memory;
buffer3->setElementSize(sizeof(int));
buffer3->setDimension(, num);
buffer3->init(); osg::ref_ptr<MyModule> module = new MyModule;
module->setBuffer1(buffer1.get());
module->setBuffer2(buffer2.get());
module->setBuffer3(buffer3.get());
module->init(); unsigned int * bufferPtr1 = (unsigned int *)(buffer1->map(osgCompute::MAP_HOST_TARGET));
memcpy(bufferPtr1, a, sizeof(a)); unsigned int * bufferPtr2 = (unsigned int *)(buffer2->map(osgCompute::MAP_HOST_TARGET));
memcpy(bufferPtr2, b, sizeof(b)); unsigned int * bufferPtr3 = (unsigned int *)(buffer3->map(osgCompute::MAP_HOST_TARGET));
memcpy(bufferPtr3, c, sizeof(c));
module->launch(); bufferPtr3 = (unsigned int *)(buffer3->map(osgCompute::MAP_HOST_SOURCE));
std::cout << "并行计算:(1, 2, 3)于(4, 5, 6)的和:"<< std::endl;
for (unsigned int i = ; i < buffer3->getDimension(); ++i)
{
std::cout << bufferPtr3[i] << std::endl;
} }
#include <cuda_runtime.h>
__global__ void addKernel(int * bytes1, int * bytes2, int * bytes3)
{
int tid = blockIdx.x;
bytes3[tid] = bytes2[tid] + bytes1[tid];
}
extern "C"
void MyCudaTest(unsigned int numBlocks, unsigned int numThreads, void * bytes1, void * bytes2, void * bytes3)
{
addKernel<<<numBlocks, numThreads>>>((int *)bytes1, (int *)bytes2, (int *)bytes3);
}

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. 64位win7系统中vb工程显示加载MSCOMCTL.OCX失败

    MSCOMCTL.OCX明明已经注册成功,但还是提示加载失败,对象未注册 尝试过的方法:system32中注册,syswow64中注册,vb打sp6补丁, 修改工程文件:用记事本打开VBP文件找到这一 ...

  2. xml-DTD相关

    DTD约束既可以作为一个单独的文件,也可以在XML文件内编写. 在XML文件内编写DTD:

  3. Rails 执行 rails server 报错 Could not find a JavaScript runtime

    gem install 'execj' gem install 'therubyrace' Ubuntu install Node.js(ubuntu) sudo apt-get install no ...

  4. iOS中的布局

    1.UIView 有三个比较重要的布局属性: frame , bounds 和 center , CALayer 对应地叫做 frame , bounds 和 position .为了能清楚区分,图层 ...

  5. android studio 工程设置项

    1.在<工程根目录>\build\generated文件夹中 存在 xxx.jar 这个文件是用来做单元测试用的,但该功能目前还处于实验阶段,不想用可以关了. 去掉勾后,clean一下工程 ...

  6. CSU 1812 三角形和矩形

    湖南省第十二届大学生计算机程序设计竞赛$J$题 计算几何. #pragma comment(linker, "/STACK:1024000000,1024000000") #inc ...

  7. XTU 1252 Defense Tower

    $2016$长城信息杯中国大学生程序设计竞赛中南邀请赛$J$题 贪心. 优先删除$power$大的点. #pragma comment(linker, "/STACK:1024000000, ...

  8. 爬虫代码实现五:解析所有分页url并优化解析实现类

    如图,我们进入优酷首页,可以看到电视剧列表,我们称这个页面为电视剧列表页,而点击进入某个电视剧,则称为电视剧详情页.那么如何获取所有分页以及对应的详情页呢,通过下面的分页得到. 因此,首先,我们将St ...

  9. UltraISO PE(软碟通) V9.5.5.2960 官方中文版

    软件名称: UltraISO PE(软碟通)软件语言: 简体中文授权方式: 免费试用运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 1.9MB图片预览: 软件简介 ...

  10. 写入XML文件

    public static void writeXMLFile(Document doc,String xmlFileName) throws IOException{  OutputFormat f ...