#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. delphi 输入文件相对路径的更改,更改成用户的

  2. 【ARM】S3C6410芯片的启动流程

    S3C6410芯片的启动流程 (1) 上电后首先运行iRom(BL0)内的代码,主要完成时钟和看门狗等外围器件的初始化.(2) 拷贝SD卡或者NnadFlash中的前4k(BL1)代码到片内ram(垫 ...

  3. 【IE6的疯狂之十】父级使用padding后子元素绝对定位的BUG

    在前端开发中,经常会用到css的position:absolute来使层浮动,前通过left,top,right等属性来对层进行定位,但ie6对left,top,right等属性的解释和ie7,ie8 ...

  4. 【Android-UI】包含多个子View时触发父节点的焦点事件

    今天遇到个问题: 在 LinearLayout 中添加了好几个其他视图 View 之后,点击时不能获得焦点,导致绑定的 onClick 事件不能触发. 解决办法: 对 LinearLayout 添加属 ...

  5. web service client端调用服务器接口

    打开项目的web service client 其中wsdl URL    http://www.51testing.com/html/55/67755-848510.html 去这里面查找一些公开的 ...

  6. jetbrain phpstorm 增加或删除一个 live template

    打开\.PhpStorm2016.2\config\templates 将xml文件放入该文件夹中 重启! 单独: setting -> editor -> Live Template +

  7. php 学习之对象

    php中怎么实现创建一个对象然后全局都能调用? 在PHP中相当常见的一种情形时,我们只需要创建一个对象一次,然后在我们的整个程序中使用它.一个很好的例子就是smarty变量,一旦被初始化后就可以在任何 ...

  8. underScore学习1:匿名函数中call(this)的作用

    匿名函数中(function(){}).call(this) 中的.call(this) 有什么用? 我们都知道,.call()可以改变函数执行时的context,即this的指向,源码中的.call ...

  9. Oracle Day 08 游标与例外的总结

    1.游标的使用(cursor) 基本格式: 定义游标:  cursor 游标名 is select语句; 打开游标:  open 游标名; loop(循环) fetch ... into ...;   ...

  10. 两个队列实现一个栈,剑指offer P59

    public class StackByQueue { private LinkedList<String> queue1; private LinkedList<String> ...