Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion brian2cuda/binomial.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
import numpy as np

from brian2.core.functions import DEFAULT_FUNCTIONS
from brian2.units.fundamentalunits import check_units
from brian2.utils.stringtools import replace
from brian2.input.binomial import (BinomialFunction, _pre_calc_constants,
_pre_calc_constants_approximated)
from brian2 import prefs
#ths can be removed when brian2 is updated to forward compiler_kwds from target generators
_CUDA_BINOMIAL_COMPILER_KWDS = {
"headers": [
'"rand.h"',
"<curand_kernel.h>",
],
}


def _generate_cuda_code(n, p, use_normal, name):
Expand Down Expand Up @@ -103,3 +109,15 @@ def _generate_cuda_code(n, p, use_normal, name):


BinomialFunction.implementations['cuda'] = _generate_cuda_code

_original_binomial_init = BinomialFunction.__init__


def _binomial_init_with_cuda_headers(self, n, p, approximate=True, name="_binomial*"):
_original_binomial_init(self, n, p, approximate=approximate, name=name)
cuda_impl = self.implementations._implementations.get("cuda")
if cuda_impl is not None:
cuda_impl.compiler_kwds = dict(_CUDA_BINOMIAL_COMPILER_KWDS)


BinomialFunction.__init__ = _binomial_init_with_cuda_headers
3 changes: 1 addition & 2 deletions brian2cuda/brianlib/clocks.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef _BRIAN_CLOCKS_H
#define _BRIAN_CLOCKS_H
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<brianlib/stdint_compat.h>
#include<math.h>
Expand Down Expand Up @@ -75,4 +74,4 @@ class EventClock : public BaseClock
}
};

#endif
#endif
29 changes: 14 additions & 15 deletions brian2cuda/brianlib/cuda_utils.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#ifndef BRIAN2CUDA_ERROR_CHECK_H
#define BRIAN2CUDA_ERROR_CHECK_H
#include <stdio.h>
#include <thrust/system_error.h>
#include "objects.h"
#include "curand.h"
#include <cuda_runtime.h>

namespace brian{
extern size_t used_device_memory;
}

// Define this to turn on error checking
#define BRIAN2CUDA_ERROR_CHECK
Expand All @@ -23,9 +25,9 @@
#define THRUST_CHECK_ERROR(code) { try {code;} \
catch(...) {_thrustCheckError(__FILE__, __LINE__, #code);} }


#ifdef BRIAN2CUDA_CURAND_HOST
#include <curand.h>
// adapted from NVIDIA cuda samples, shipped with cuda 10.1 (common/inc/helper_cuda.h)
#ifdef CURAND_H_
// cuRAND API errors
static const char *_curandGetErrorEnum(curandStatus_t error) {
switch (error) {
Expand Down Expand Up @@ -71,39 +73,37 @@ static const char *_curandGetErrorEnum(curandStatus_t error) {

return "<unknown>";
}
#endif


inline void _cudaSafeCall(cudaError err, const char *file, const int line, const char *call = "")
inline void _cudaSafeCall(curandStatus_t err, const char *file, const int line, const char *call = "")
{
#ifdef BRIAN2CUDA_ERROR_CHECK
if (cudaSuccess != err)
if (CURAND_STATUS_SUCCESS != err)
{
fprintf(stderr, "ERROR: %s failed at %s:%i : %s\n",
call, file, line, cudaGetErrorString(err));
call, file, line, _curandGetErrorEnum(err));
exit(-1);
}
#endif

return;
}
#endif // BRIAN2CUDA_CURAND_HOST


inline void _cudaSafeCall(curandStatus_t err, const char *file, const int line, const char *call = "")
inline void _cudaSafeCall(cudaError err, const char *file, const int line, const char *call = "")
{
#ifdef BRIAN2CUDA_ERROR_CHECK
if (CURAND_STATUS_SUCCESS != err)
if (cudaSuccess != err)
{
fprintf(stderr, "ERROR: %s failed at %s:%i : %s\n",
call, file, line, _curandGetErrorEnum(err));
call, file, line, cudaGetErrorString(err));
exit(-1);
}
#endif

return;
}


inline void _cudaCheckError(const char *file, const int line, const char *msg)
{
#ifdef BRIAN2CUDA_ERROR_CHECK_BLOCKING
Expand Down Expand Up @@ -176,5 +176,4 @@ inline void _thrustCheckError(const char *file, const int line,
code, file, line);
throw;
}

#endif // BRIAN2CUDA_ERROR_CHECK_H
1 change: 0 additions & 1 deletion brian2cuda/brianlib/spikequeue.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include<iostream>
#include<vector>
#include<algorithm>
#include<inttypes.h>
Expand Down
8 changes: 6 additions & 2 deletions brian2cuda/codeobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,19 @@ class CUDAStandaloneAtomicsCodeObject(CUDAStandaloneCodeObject):
rand_code = '''
#define _rand(vectorisation_idx) (_ptr_array_%CODEOBJ_NAME%_rand[vectorisation_idx])
'''

_RAND_HEADERS = {"headers": ['"rand.h"']}
rand_impls = DEFAULT_FUNCTIONS['rand'].implementations
rand_impls.add_implementation(CUDAStandaloneCodeObject,
code=rand_code,
name='_rand')
name='_rand',
compiler_kwds=_RAND_HEADERS)

randn_code = '''
#define _randn(vectorisation_idx) (_ptr_array_%CODEOBJ_NAME%_randn[vectorisation_idx])
'''
randn_impls = DEFAULT_FUNCTIONS['randn'].implementations
randn_impls.add_implementation(CUDAStandaloneCodeObject,
code=randn_code,
name='_randn')
name='_randn',
compiler_kwds=_RAND_HEADERS)
8 changes: 7 additions & 1 deletion brian2cuda/cuda_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,13 @@ class CUDAAtomicsCodeGenerator(CUDACodeGenerator):
CUDACodeGenerator,
code=poisson_code,
name='_poisson',
compiler_kwds={"headers": ["<curand.h>"]}
compiler_kwds={
"headers": [
"<curand.h>",
"<curand_kernel.h>",
'"rand.h"',
],
}
)


Expand Down
Loading
Loading