1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
/*******************************************************
* Copyright (c) 2015-2019, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <forge.h>
#include <cuda_runtime.h>
#include <cuComplex.h>
#define USE_FORGE_CUDA_COPY_HELPERS
#include <ComputeCopy.h>
#include <cstdio>
#include <iostream>
const unsigned DIMX = 1000;
const unsigned DIMY = 800;
static const float dx = 0.1;
static const float FRANGE_START = 0.f;
static const float FRANGE_END = 2 * 3.141592f;
static const size_t DATA_SIZE = ( FRANGE_END - FRANGE_START ) / dx;
void kernel(float* dev_out, int functionCode);
int main(void)
{
float *sin_out;
float *cos_out;
float *tan_out;
float *log_out;
/*
* First Forge call should be a window creation call
* so that necessary OpenGL context is created for any
* other forge::* object to be created successfully
*/
forge::Window wnd(DIMX, DIMY, "Plotting Demo");
wnd.makeCurrent();
forge::Chart chart(FG_CHART_2D);
chart.setAxesLimits(FRANGE_START, FRANGE_END, -1.0f, 1.0f);
/* Create several plot objects which creates the necessary
* vertex buffer objects to hold the different plot types
*/
forge::Plot plt0 = chart.plot( DATA_SIZE, forge::f32); //create a default plot
forge::Plot plt1 = chart.plot( DATA_SIZE, forge::f32, FG_PLOT_LINE, FG_MARKER_NONE); //or specify a specific plot type
forge::Plot plt2 = chart.plot( DATA_SIZE, forge::f32, FG_PLOT_LINE, FG_MARKER_TRIANGLE); //last parameter specifies marker shape
forge::Plot plt3 = chart.plot( DATA_SIZE, forge::f32, FG_PLOT_SCATTER, FG_MARKER_CROSS);
/*
* Set plot colors
*/
plt0.setColor(FG_RED);
plt1.setColor(FG_BLUE);
plt2.setColor(FG_YELLOW); //use a forge predefined color
plt3.setColor((forge::Color) 0x257973FF); //or any hex-valued color
/*
* Set plot legends
*/
plt0.setLegend("Sine");
plt1.setLegend("Cosine");
plt2.setLegend("Tangent");
plt3.setLegend("Log base 10");
FORGE_CUDA_CHECK(cudaMalloc((void**)&sin_out, sizeof(float) * DATA_SIZE * 2));
FORGE_CUDA_CHECK(cudaMalloc((void**)&cos_out, sizeof(float) * DATA_SIZE * 2));
FORGE_CUDA_CHECK(cudaMalloc((void**)&tan_out, sizeof(float) * DATA_SIZE * 2));
FORGE_CUDA_CHECK(cudaMalloc((void**)&log_out, sizeof(float) * DATA_SIZE * 2));
kernel(sin_out, 0);
kernel(cos_out, 1);
kernel(tan_out, 2);
kernel(log_out, 3);
GfxHandle* handles[4];
createGLBuffer(&handles[0], plt0.vertices(), FORGE_VERTEX_BUFFER);
createGLBuffer(&handles[1], plt1.vertices(), FORGE_VERTEX_BUFFER);
createGLBuffer(&handles[2], plt2.vertices(), FORGE_VERTEX_BUFFER);
createGLBuffer(&handles[3], plt3.vertices(), FORGE_VERTEX_BUFFER);
/* copy your data into the vertex buffer object exposed by
* forge::Plot class and then proceed to rendering.
* To help the users with copying the data from compute
* memory to display memory, Forge provides copy headers
* along with the library to help with this task
*/
copyToGLBuffer(handles[0], (ComputeResourceHandle)sin_out, plt0.verticesSize());
copyToGLBuffer(handles[1], (ComputeResourceHandle)cos_out, plt1.verticesSize());
copyToGLBuffer(handles[2], (ComputeResourceHandle)tan_out, plt2.verticesSize());
copyToGLBuffer(handles[3], (ComputeResourceHandle)log_out, plt3.verticesSize());
do {
wnd.draw(chart);
} while(!wnd.close());
FORGE_CUDA_CHECK(cudaFree(sin_out));
FORGE_CUDA_CHECK(cudaFree(cos_out));
FORGE_CUDA_CHECK(cudaFree(tan_out));
FORGE_CUDA_CHECK(cudaFree(log_out));
releaseGLBuffer(handles[0]);
releaseGLBuffer(handles[1]);
releaseGLBuffer(handles[2]);
releaseGLBuffer(handles[3]);
return 0;
}
__global__
void simple_sinf(float* out, const size_t _data_size, int fnCode, const float _dx, const float _frange_start)
{
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < _data_size) {
float x = _frange_start + i * _dx;
int idx = 2 * i;
out[idx] = x;
switch(fnCode) {
case 0:
out[ idx + 1 ] = sinf(x);
break;
case 1:
out[ idx + 1 ] = cosf(x);
break;
case 2:
out[ idx + 1 ] = tanf(x);
break;
case 3:
out[ idx + 1 ] = log10f(x);
break;
}
}
}
inline int divup(int a, int b)
{
return (a+b-1)/b;
}
void kernel(float* dev_out, int functionCode)
{
static const dim3 threads(1024);
dim3 blocks(divup(DATA_SIZE, 1024));
simple_sinf << < blocks, threads >> >(dev_out, DATA_SIZE, functionCode, dx, FRANGE_START);
}
|