summaryrefslogtreecommitdiff
path: root/graphics/asymptote/cudareflect/helper.cuh
blob: 87d1bb6aebdead2fbc7f9c828f352de5b7ebde07 (plain)
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
#pragma once

#include <iostream>
#include <cuda.h>
#include <cuda_runtime.h>

inline void check_error()
{
#ifdef DEBUG
    cudaError_t __err = cudaGetLastError();
    if (__err != cudaSuccess)
    {
        std::cerr << "Fata CUDA Error. Msg: " << cudaGetErrorString(__err) << std::endl;
    }
    else
    {
        std::cerr << "CUDA Operation success" << std::endl;
    }
#endif
}

inline void cudaErrorCheck(cudaError err)
{
    if (err != cudaSuccess)
    {
        std::cerr << "ERROR: " << cudaGetErrorString(err) << std::endl;
        exit(1);
    }
}

template<typename T>
T* cudaDupe(T* in, size_t sz)
{
    T* out = nullptr;
    cudaErrorCheck(cudaMalloc((void**)&out, sizeof(T) * sz));
    cudaErrorCheck(cudaMemcpy(out, in, sizeof(T) * sz, cudaMemcpyHostToDevice));
    return out;
}

__device__
inline int to_idx(int width, int x, int y)
{
    return y * width + x;
}