Sample Code
From ShWiki
(Difference between revisions)
| Revision as of 22:08, 13 October 2005 Francois (Talk | contribs) Missing word ← Previous diff |
Revision as of 14:56, 10 December 2005 Francois (Talk | contribs) Format c++ code nicely Next diff → |
||
| Line 4: | Line 4: | ||
| Here is a minimal stream program showing how to create storage for program inputs and outputs. | Here is a minimal stream program showing how to create storage for program inputs and outputs. | ||
| + | <cpp>#include "sh.hpp" | ||
| + | #include <iostream> | ||
| - | <pre><nowiki> | + | using namespace std; |
| - | #include "sh.hpp" | + | using namespace SH; |
| - | #include <iostream> | + | |
| - | using namespace std; | + | int main() |
| - | using namespace SH; | + | { |
| + | ShProgram prg = SH_BEGIN_PROGRAM("gpu:stream") { | ||
| + | ShInputAttrib3f a; | ||
| + | ShOutputAttrib3f b; | ||
| + | b = a + ShAttrib3f(42.0, 42.0, 42.0); | ||
| + | } SH_END; | ||
| - | int main() | + | float data[] = { 1.0, 0.5, -0.5 }; |
| - | { | + | ShHostMemoryPtr mem_in = new ShHostMemory(sizeof(float) * 3, data, SH_FLOAT); |
| - | ShProgram prg = SH_BEGIN_PROGRAM("gpu:stream") { | + | ShChannel<ShAttrib3f> in(mem_in, 1); |
| - | ShInputAttrib3f a; | + | |
| - | ShOutputAttrib3f b; | + | |
| - | b = a + ShAttrib3f(42.0, 42.0, 42.0); | + | |
| - | } SH_END; | + | |
| - | float data[] = { 1.0, 0.5, -0.5 }; | + | float outdata[3]; |
| - | ShHostMemoryPtr mem_in = new ShHostMemory(sizeof(float) * 3, data, SH_FLOAT); | + | ShHostMemoryPtr mem_out = new ShHostMemory(sizeof(float) * 3, outdata, SH_FLOAT); |
| - | ShChannel<ShAttrib3f> in(mem_in, 1); | + | ShChannel<ShAttrib3f> out(mem_out, 1); |
| - | float outdata[3]; | + | out = prg << in; |
| - | ShHostMemoryPtr mem_out = new ShHostMemory(sizeof(float) * 3, outdata, SH_FLOAT); | + | |
| - | ShChannel<ShAttrib3f> out(mem_out, 1); | + | |
| - | + | ||
| - | out = prg << in; | + | |
| - | float* results = static_cast<float*>(mem_out->hostStorage()->data()); | + | float* results = static_cast<float*>(mem_out->hostStorage()->data()); |
| - | cout << "out = (" << results[0] << ", " << results[1] << ", " | + | cout << "out = (" << results[0] << ", " << results[1] << ", " |
| - | << results[2] << ")" << endl; | + | << results[2] << ")" << endl; |
| - | } | + | }</cpp> |
| - | </nowiki></pre> | + | |
| It should simply output the following: | It should simply output the following: | ||
| out = (43, 42.5, 41.5) | out = (43, 42.5, 41.5) | ||
Revision as of 14:56, 10 December 2005
Here are some examples of programs which use Sh. Feel free to suggest additions to this page if you want.
Simple stream program
Here is a minimal stream program showing how to create storage for program inputs and outputs.
#include "sh.hpp" #include <iostream> using namespace std; using namespace SH; int main() { ShProgram prg = SH_BEGIN_PROGRAM("gpu:stream") { ShInputAttrib3f a; ShOutputAttrib3f b; b = a + ShAttrib3f(42.0, 42.0, 42.0); } SH_END; float data[] = { 1.0, 0.5, -0.5 }; ShHostMemoryPtr mem_in = new ShHostMemory(sizeof(float) * 3, data, SH_FLOAT); ShChannel<ShAttrib3f> in(mem_in, 1); float outdata[3]; ShHostMemoryPtr mem_out = new ShHostMemory(sizeof(float) * 3, outdata, SH_FLOAT); ShChannel<ShAttrib3f> out(mem_out, 1); out = prg << in; float* results = static_cast<float*>(mem_out->hostStorage()->data()); cout << "out = (" << results[0] << ", " << results[1] << ", " << results[2] << ")" << endl; }
It should simply output the following:
out = (43, 42.5, 41.5)
