#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. Kafka集群搭建

    1.zookeeper搭建 Kafka集群依赖zookeeper,需要提前搭建好zookeeper zookeeper快速搭建推荐地址:http://nileader.blog.51cto.com/1 ...

  2. lua中的面向对象编程

    简单说说Lua中的面向对象 Lua中的table就是一种对象,看以下一段简单的代码: 上述代码会输出tb1 ~= tb2.说明两个具有相同值得对象是两个不同的对象,同时在Lua中table是引用类型的 ...

  3. 前端知识点-JS相关知识点

    1.谈谈你对Ajax的理解?(概念.特点.作用) AJAX全称为"Asynchronous JavaScript And XML"(异步JavaScript和XML) 是指一种创建 ...

  4. java 获取URL链接 内容

    public static String getHtmlConentByUrl(String ssourl) {        try {             URL url = new URL( ...

  5. 前端tab页实例

    <div class="tabbable"> <ul class="nav nav-tabs padding-16"> <c:fo ...

  6. 在一个form里边同时执行搜索和 execl导出功能

    一个form 分搜索 和 导出<form name="searchform" id="searchform" > <input type=&q ...

  7. 面试题-Java Web-Servlet部分

    1.什么是Servlet? Servlet是用来处理客户端请求并产生动态网页内容的Java类.Servlet主要是用来处理或者是存储HTML表单提交的数据,产生动态内容,在无状态的HTTP协议下管理状 ...

  8. CUDA编程入门,Dim3变量

    dim3是NVIDIA的CUDA编程中一种自定义的整型向量类型,基于用于指定维度的uint3. 例如:dim3 grid(num1,num2,num3): dim3类型最终设置的是一个三维向量,三维参 ...

  9. 在eclipse中maven web项目部署到tomcat,访问不了

    修改eclipse中tomcat发布路径后,能正常访问

  10. JMeter+ant+jenkins自动化持续集成

    一.ant安装配置 1.官网下载地址:http://ant.apache.org/bindownload.cgi 对应的操作系统选择对应的版本下载,本文以windows为列,下载后解压到本地 2.设置 ...