Giter Site home page Giter Site logo

cuda-arrays's Introduction

CuArrays: Multidimensional cuda arrays with proper memory management

This is a headers only library. To use it, simple include CuArrays in your .cu or .cpp files. See the example below.

To compile, add the cuda-arrays folder to your include path, e.g.:

nvcc -I$HOME/local/include/cuda-arrays example.cu

For other compilers don't forget to include the CUDA runtime headers:

g++ -I$HOME/local/include/cuda-arrays \
    -I/usr/local/cuda/include example.cpp

Example:

#include <CuArrays>

namespace Device
{
    // Arrays only contain the pointers to global device memory, so they
    // can be stored in constant device memory for quick access:
    __constant__ Array<double>   test1d;
    __constant__ Array2D<double> test2d;
}

__global__ void test_kernel()
{
    using namespace Device;

    int i = threadIdx.x;
    int j = threadIdx.y;

    if (j == 0)
        test1d(i) *= 2;

    test2d(i,j) *= 5;
}

int main()
{
    // link device and host arrays
    ManagedArray<double>   test1d(Device::test1d);
    ManagedArray2D<double> test2d(Device::test2d);

    // allocate memory (both on host and device)
    test1d.malloc(10);
    test2d.malloc(10,10);

    // fill host with data
    for (int i = 0; i < 10; i++)
    {
        test1d(i) = i;
        for (int j = 0; j < 10; j++)
        {
            test2d(i, j) = i + j;
        }
    }

    // copy to device
    test1d.copyToDeviceAsync();
    test2d.copyToDeviceAsync();

    // run test kernel
    test_kernel<<<1, dim3(10,10) >>>();

    // copy data back
    test1d.copyFromDeviceAsync();
    test2d.copyFromDeviceAsync();

    // output 1D array
    printf("test1d = ");
    for (int i = 0; i < 10; i++)
        printf("%.2e ", test1d(i));
    printf("\n\n");

    // output 2D array
    for (int i = 0; i < 10; i++)
    {
        printf("test2d[%i] = ", i);
        for (int j = 0; j < 10; j++)
        {
            printf("%.2e ", test2d(i, j));
        }
        printf("\n");
    }

    // free all memory
    test1d.free();
    test2d.free();

    return 0;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.