neuron::neuron(neuralNet& parent, size_t numWeights) : iWeightBegin(SIZE_MAX), iWeightEnd(SIZE_MAX), iNeuronIndex(SIZE_MAX) { size_t _iWeightBegin = parent.weights.size(); if (!IS_ALIGNED(_iWeightBegin)) { _iWeightBegin += ALIGN_SIZE - ALIGN_OF(_iWeightBegin); } // set weight array begin and end: iWeightBegin = _iWeightBegin; iWeightEnd = iWeightBegin + numWeights; // allow 1 value for bias weight at the end parent.weights.resize(iWeightBegin + ((numWeights>0)?(numWeights+1):0), 0.0f); // always one value allocated for activations: iNeuronIndex = parent.activations.size(); parent.activations.push_back(0.0f); };
experienceNet::experienceNet(size_t _numFeatures, const experienceNetSettings& _cSet) : vals(), offsets(), eSet(_cSet), expAdd(_cSet.eType==EXPERIENCENET_HISTOGRAM?frequencyAdd_sse:_cSet.eType==EXPERIENCENET_RECENCY?recencyAdd_sse:NULL), numFeatures(_numFeatures), partition((1.0f) / ((float)eSet.numTaps - 1)), maxNormalRecip(1.0f / boost::math::pdf(boost::math::normal_distribution<float>(0.0f, eSet.extrapolation), 0.0f)) { assert(expAdd != NULL); assert(numFeatures > 0); size_t expandedTapSize = eSet.numTaps; if (!IS_ALIGNED(expandedTapSize)) { expandedTapSize += ALIGN_SIZE - ALIGN_OF(expandedTapSize); } // fill val array: vals.assign(expandedTapSize * numFeatures, 0.0f); // assign offset array: offsets.reserve(numFeatures); for(size_t iFeature = 0; iFeature != numFeatures; ++iFeature) { offsets.push_back(iFeature * expandedTapSize); } };
void Init_fiddle(void) { /* * Document-module: Fiddle * * A libffi wrapper for Ruby. * * == Description * * Fiddle is an extension to translate a foreign function interface (FFI) * with ruby. * * It wraps {libffi}[http://sourceware.org/libffi/], a popular C library * which provides a portable interface that allows code written in one * language to call code written in another language. * * == Example * * Here we will use Fiddle::Function to wrap {floor(3) from * libm}[http://linux.die.net/man/3/floor] * * require 'fiddle' * * libm = Fiddle.dlopen('/lib/libm.so.6') * * floor = Fiddle::Function.new( * libm['floor'], * [Fiddle::TYPE_DOUBLE], * Fiddle::TYPE_DOUBLE * ) * * puts floor.call(3.14159) #=> 3.0 * * */ mFiddle = rb_define_module("Fiddle"); /* * Document-class: Fiddle::DLError * * standard dynamic load exception */ rb_eFiddleError = rb_define_class_under(mFiddle, "DLError", rb_eStandardError); /* Document-const: TYPE_VOID * * C type - void */ rb_define_const(mFiddle, "TYPE_VOID", INT2NUM(TYPE_VOID)); /* Document-const: TYPE_VOIDP * * C type - void* */ rb_define_const(mFiddle, "TYPE_VOIDP", INT2NUM(TYPE_VOIDP)); /* Document-const: TYPE_CHAR * * C type - char */ rb_define_const(mFiddle, "TYPE_CHAR", INT2NUM(TYPE_CHAR)); /* Document-const: TYPE_SHORT * * C type - short */ rb_define_const(mFiddle, "TYPE_SHORT", INT2NUM(TYPE_SHORT)); /* Document-const: TYPE_INT * * C type - int */ rb_define_const(mFiddle, "TYPE_INT", INT2NUM(TYPE_INT)); /* Document-const: TYPE_LONG * * C type - long */ rb_define_const(mFiddle, "TYPE_LONG", INT2NUM(TYPE_LONG)); #if HAVE_LONG_LONG /* Document-const: TYPE_LONG_LONG * * C type - long long */ rb_define_const(mFiddle, "TYPE_LONG_LONG", INT2NUM(TYPE_LONG_LONG)); #endif /* Document-const: TYPE_FLOAT * * C type - float */ rb_define_const(mFiddle, "TYPE_FLOAT", INT2NUM(TYPE_FLOAT)); /* Document-const: TYPE_DOUBLE * * C type - double */ rb_define_const(mFiddle, "TYPE_DOUBLE", INT2NUM(TYPE_DOUBLE)); /* Document-const: TYPE_SIZE_T * * C type - size_t */ rb_define_const(mFiddle, "TYPE_SIZE_T", INT2NUM(TYPE_SIZE_T)); /* Document-const: TYPE_SSIZE_T * * C type - ssize_t */ rb_define_const(mFiddle, "TYPE_SSIZE_T", INT2NUM(TYPE_SSIZE_T)); /* Document-const: TYPE_PTRDIFF_T * * C type - ptrdiff_t */ rb_define_const(mFiddle, "TYPE_PTRDIFF_T", INT2NUM(TYPE_PTRDIFF_T)); /* Document-const: TYPE_INTPTR_T * * C type - intptr_t */ rb_define_const(mFiddle, "TYPE_INTPTR_T", INT2NUM(TYPE_INTPTR_T)); /* Document-const: TYPE_UINTPTR_T * * C type - uintptr_t */ rb_define_const(mFiddle, "TYPE_UINTPTR_T", INT2NUM(TYPE_UINTPTR_T)); /* Document-const: ALIGN_VOIDP * * The alignment size of a void* */ rb_define_const(mFiddle, "ALIGN_VOIDP", INT2NUM(ALIGN_VOIDP)); /* Document-const: ALIGN_CHAR * * The alignment size of a char */ rb_define_const(mFiddle, "ALIGN_CHAR", INT2NUM(ALIGN_CHAR)); /* Document-const: ALIGN_SHORT * * The alignment size of a short */ rb_define_const(mFiddle, "ALIGN_SHORT", INT2NUM(ALIGN_SHORT)); /* Document-const: ALIGN_INT * * The alignment size of an int */ rb_define_const(mFiddle, "ALIGN_INT", INT2NUM(ALIGN_INT)); /* Document-const: ALIGN_LONG * * The alignment size of a long */ rb_define_const(mFiddle, "ALIGN_LONG", INT2NUM(ALIGN_LONG)); #if HAVE_LONG_LONG /* Document-const: ALIGN_LONG_LONG * * The alignment size of a long long */ rb_define_const(mFiddle, "ALIGN_LONG_LONG", INT2NUM(ALIGN_LONG_LONG)); #endif /* Document-const: ALIGN_FLOAT * * The alignment size of a float */ rb_define_const(mFiddle, "ALIGN_FLOAT", INT2NUM(ALIGN_FLOAT)); /* Document-const: ALIGN_DOUBLE * * The alignment size of a double */ rb_define_const(mFiddle, "ALIGN_DOUBLE",INT2NUM(ALIGN_DOUBLE)); /* Document-const: ALIGN_SIZE_T * * The alignment size of a size_t */ rb_define_const(mFiddle, "ALIGN_SIZE_T", INT2NUM(ALIGN_OF(size_t))); /* Document-const: ALIGN_SSIZE_T * * The alignment size of a ssize_t */ rb_define_const(mFiddle, "ALIGN_SSIZE_T", INT2NUM(ALIGN_OF(size_t))); /* same as size_t */ /* Document-const: ALIGN_PTRDIFF_T * * The alignment size of a ptrdiff_t */ rb_define_const(mFiddle, "ALIGN_PTRDIFF_T", INT2NUM(ALIGN_OF(ptrdiff_t))); /* Document-const: ALIGN_INTPTR_T * * The alignment size of a intptr_t */ rb_define_const(mFiddle, "ALIGN_INTPTR_T", INT2NUM(ALIGN_OF(intptr_t))); /* Document-const: ALIGN_UINTPTR_T * * The alignment size of a uintptr_t */ rb_define_const(mFiddle, "ALIGN_UINTPTR_T", INT2NUM(ALIGN_OF(uintptr_t))); /* Document-const: WINDOWS * * Returns a boolean regarding whether the host is WIN32 */ #if defined(_WIN32) rb_define_const(mFiddle, "WINDOWS", Qtrue); #else rb_define_const(mFiddle, "WINDOWS", Qfalse); #endif /* Document-const: SIZEOF_VOIDP * * size of a void* */ rb_define_const(mFiddle, "SIZEOF_VOIDP", INT2NUM(sizeof(void*))); /* Document-const: SIZEOF_CHAR * * size of a char */ rb_define_const(mFiddle, "SIZEOF_CHAR", INT2NUM(sizeof(char))); /* Document-const: SIZEOF_SHORT * * size of a short */ rb_define_const(mFiddle, "SIZEOF_SHORT", INT2NUM(sizeof(short))); /* Document-const: SIZEOF_INT * * size of an int */ rb_define_const(mFiddle, "SIZEOF_INT", INT2NUM(sizeof(int))); /* Document-const: SIZEOF_LONG * * size of a long */ rb_define_const(mFiddle, "SIZEOF_LONG", INT2NUM(sizeof(long))); #if HAVE_LONG_LONG /* Document-const: SIZEOF_LONG_LONG * * size of a long long */ rb_define_const(mFiddle, "SIZEOF_LONG_LONG", INT2NUM(sizeof(LONG_LONG))); #endif /* Document-const: SIZEOF_FLOAT * * size of a float */ rb_define_const(mFiddle, "SIZEOF_FLOAT", INT2NUM(sizeof(float))); /* Document-const: SIZEOF_DOUBLE * * size of a double */ rb_define_const(mFiddle, "SIZEOF_DOUBLE",INT2NUM(sizeof(double))); /* Document-const: SIZEOF_SIZE_T * * size of a size_t */ rb_define_const(mFiddle, "SIZEOF_SIZE_T", INT2NUM(sizeof(size_t))); /* Document-const: SIZEOF_SSIZE_T * * size of a ssize_t */ rb_define_const(mFiddle, "SIZEOF_SSIZE_T", INT2NUM(sizeof(size_t))); /* same as size_t */ /* Document-const: SIZEOF_PTRDIFF_T * * size of a ptrdiff_t */ rb_define_const(mFiddle, "SIZEOF_PTRDIFF_T", INT2NUM(sizeof(ptrdiff_t))); /* Document-const: SIZEOF_INTPTR_T * * size of a intptr_t */ rb_define_const(mFiddle, "SIZEOF_INTPTR_T", INT2NUM(sizeof(intptr_t))); /* Document-const: SIZEOF_UINTPTR_T * * size of a uintptr_t */ rb_define_const(mFiddle, "SIZEOF_UINTPTR_T", INT2NUM(sizeof(uintptr_t))); /* Document-const: RUBY_FREE * * Address of the ruby_xfree() function */ rb_define_const(mFiddle, "RUBY_FREE", PTR2NUM(ruby_xfree)); /* Document-const: BUILD_RUBY_PLATFORM * * Platform built against (i.e. "x86_64-linux", etc.) * * See also RUBY_PLATFORM */ rb_define_const(mFiddle, "BUILD_RUBY_PLATFORM", rb_str_new2(RUBY_PLATFORM)); rb_define_module_function(mFiddle, "dlwrap", rb_fiddle_value2ptr, 1); rb_define_module_function(mFiddle, "dlunwrap", rb_fiddle_ptr2value, 1); rb_define_module_function(mFiddle, "malloc", rb_fiddle_malloc, 1); rb_define_module_function(mFiddle, "realloc", rb_fiddle_realloc, 2); rb_define_module_function(mFiddle, "free", rb_fiddle_free, 1); Init_fiddle_function(); Init_fiddle_closure(); Init_fiddle_handle(); Init_fiddle_pointer(); }
void Init_dl(void) { void Init_dlhandle(void); void Init_dlcfunc(void); void Init_dlptr(void); rbdl_id_cdecl = rb_intern_const("cdecl"); rbdl_id_stdcall = rb_intern_const("stdcall"); /* Document-module: DL * * A bridge to the dlopen() or dynamic library linker function. * * == Example * * bash $> cat > sum.c <<EOF * double sum(double *arry, int len) * { * double ret = 0; * int i; * for(i = 0; i < len; i++){ * ret = ret + arry[i]; * } * return ret; * } * * double split(double num) * { * double ret = 0; * ret = num / 2; * return ret; * } * EOF * bash $> gcc -o libsum.so -shared sum.c * bash $> cat > sum.rb <<EOF * require 'dl' * require 'dl/import' * * module LibSum * extend DL::Importer * dlload './libsum.so' * extern 'double sum(double*, int)' * extern 'double split(double)' * end * * a = [2.0, 3.0, 4.0] * * sum = LibSum.sum(a.pack("d*"), a.count) * p LibSum.split(sum) * EOF * bash $> ruby sum.rb * 4.5 * * WIN! :-) */ rb_mDL = rb_define_module("DL"); /* * Document-class: DL::DLError * * standard dynamic load exception */ rb_eDLError = rb_define_class_under(rb_mDL, "DLError", rb_eStandardError); /* * Document-class: DL::DLTypeError * * dynamic load incorrect type exception */ rb_eDLTypeError = rb_define_class_under(rb_mDL, "DLTypeError", rb_eDLError); /* Document-const: MAX_CALLBACK * * Maximum number of callbacks */ rb_define_const(rb_mDL, "MAX_CALLBACK", INT2NUM(MAX_CALLBACK)); /* Document-const: DLSTACK_SIZE * * Dynamic linker stack size */ rb_define_const(rb_mDL, "DLSTACK_SIZE", INT2NUM(DLSTACK_SIZE)); rb_dl_init_callbacks(rb_mDL); /* Document-const: RTLD_GLOBAL * * rtld DL::Handle flag. * * The symbols defined by this library will be made available for symbol * resolution of subsequently loaded libraries. */ rb_define_const(rb_mDL, "RTLD_GLOBAL", INT2NUM(RTLD_GLOBAL)); /* Document-const: RTLD_LAZY * * rtld DL::Handle flag. * * Perform lazy binding. Only resolve symbols as the code that references * them is executed. If the symbol is never referenced, then it is never * resolved. (Lazy binding is only performed for function references; * references to variables are always immediately bound when the library * is loaded.) */ rb_define_const(rb_mDL, "RTLD_LAZY", INT2NUM(RTLD_LAZY)); /* Document-const: RTLD_NOW * * rtld DL::Handle flag. * * If this value is specified or the environment variable LD_BIND_NOW is * set to a nonempty string, all undefined symbols in the library are * resolved before dlopen() returns. If this cannot be done an error is * returned. */ rb_define_const(rb_mDL, "RTLD_NOW", INT2NUM(RTLD_NOW)); /* Document-const: TYPE_VOID * * DL::CFunc type - void */ rb_define_const(rb_mDL, "TYPE_VOID", INT2NUM(DLTYPE_VOID)); /* Document-const: TYPE_VOIDP * * DL::CFunc type - void* */ rb_define_const(rb_mDL, "TYPE_VOIDP", INT2NUM(DLTYPE_VOIDP)); /* Document-const: TYPE_CHAR * * DL::CFunc type - char */ rb_define_const(rb_mDL, "TYPE_CHAR", INT2NUM(DLTYPE_CHAR)); /* Document-const: TYPE_SHORT * * DL::CFunc type - short */ rb_define_const(rb_mDL, "TYPE_SHORT", INT2NUM(DLTYPE_SHORT)); /* Document-const: TYPE_INT * * DL::CFunc type - int */ rb_define_const(rb_mDL, "TYPE_INT", INT2NUM(DLTYPE_INT)); /* Document-const: TYPE_LONG * * DL::CFunc type - long */ rb_define_const(rb_mDL, "TYPE_LONG", INT2NUM(DLTYPE_LONG)); #if HAVE_LONG_LONG /* Document-const: TYPE_LONG_LONG * * DL::CFunc type - long long */ rb_define_const(rb_mDL, "TYPE_LONG_LONG", INT2NUM(DLTYPE_LONG_LONG)); #endif /* Document-const: TYPE_FLOAT * * DL::CFunc type - float */ rb_define_const(rb_mDL, "TYPE_FLOAT", INT2NUM(DLTYPE_FLOAT)); /* Document-const: TYPE_DOUBLE * * DL::CFunc type - double */ rb_define_const(rb_mDL, "TYPE_DOUBLE", INT2NUM(DLTYPE_DOUBLE)); /* Document-const: TYPE_SIZE_T * * DL::CFunc type - size_t */ rb_define_const(rb_mDL, "TYPE_SIZE_T", INT2NUM(DLTYPE_SIZE_T)); /* Document-const: TYPE_SSIZE_T * * DL::CFunc type - ssize_t */ rb_define_const(rb_mDL, "TYPE_SSIZE_T", INT2NUM(DLTYPE_SSIZE_T)); /* Document-const: TYPE_PTRDIFF_T * * DL::CFunc type - ptrdiff_t */ rb_define_const(rb_mDL, "TYPE_PTRDIFF_T", INT2NUM(DLTYPE_PTRDIFF_T)); /* Document-const: TYPE_INTPTR_T * * DL::CFunc type - intptr_t */ rb_define_const(rb_mDL, "TYPE_INTPTR_T", INT2NUM(DLTYPE_INTPTR_T)); /* Document-const: TYPE_UINTPTR_T * * DL::CFunc type - uintptr_t */ rb_define_const(rb_mDL, "TYPE_UINTPTR_T", INT2NUM(DLTYPE_UINTPTR_T)); /* Document-const: ALIGN_VOIDP * * The alignment size of a void* */ rb_define_const(rb_mDL, "ALIGN_VOIDP", INT2NUM(ALIGN_VOIDP)); /* Document-const: ALIGN_CHAR * * The alignment size of a char */ rb_define_const(rb_mDL, "ALIGN_CHAR", INT2NUM(ALIGN_CHAR)); /* Document-const: ALIGN_SHORT * * The alignment size of a short */ rb_define_const(rb_mDL, "ALIGN_SHORT", INT2NUM(ALIGN_SHORT)); /* Document-const: ALIGN_INT * * The alignment size of an int */ rb_define_const(rb_mDL, "ALIGN_INT", INT2NUM(ALIGN_INT)); /* Document-const: ALIGN_LONG * * The alignment size of a long */ rb_define_const(rb_mDL, "ALIGN_LONG", INT2NUM(ALIGN_LONG)); #if HAVE_LONG_LONG /* Document-const: ALIGN_LONG_LONG * * The alignment size of a long long */ rb_define_const(rb_mDL, "ALIGN_LONG_LONG", INT2NUM(ALIGN_LONG_LONG)); #endif /* Document-const: ALIGN_FLOAT * * The alignment size of a float */ rb_define_const(rb_mDL, "ALIGN_FLOAT", INT2NUM(ALIGN_FLOAT)); /* Document-const: ALIGN_DOUBLE * * The alignment size of a double */ rb_define_const(rb_mDL, "ALIGN_DOUBLE",INT2NUM(ALIGN_DOUBLE)); /* Document-const: ALIGN_SIZE_T * * The alignment size of a size_t */ rb_define_const(rb_mDL, "ALIGN_SIZE_T", INT2NUM(ALIGN_OF(size_t))); /* Document-const: ALIGN_SSIZE_T * * The alignment size of a ssize_t */ rb_define_const(rb_mDL, "ALIGN_SSIZE_T", INT2NUM(ALIGN_OF(size_t))); /* same as size_t */ /* Document-const: ALIGN_PTRDIFF_T * * The alignment size of a ptrdiff_t */ rb_define_const(rb_mDL, "ALIGN_PTRDIFF_T", INT2NUM(ALIGN_OF(ptrdiff_t))); /* Document-const: ALIGN_INTPTR_T * * The alignment size of a intptr_t */ rb_define_const(rb_mDL, "ALIGN_INTPTR_T", INT2NUM(ALIGN_OF(intptr_t))); /* Document-const: ALIGN_UINTPTR_T * * The alignment size of a uintptr_t */ rb_define_const(rb_mDL, "ALIGN_UINTPTR_T", INT2NUM(ALIGN_OF(uintptr_t))); /* Document-const: SIZEOF_VOIDP * * size of a void* */ rb_define_const(rb_mDL, "SIZEOF_VOIDP", INT2NUM(sizeof(void*))); /* Document-const: SIZEOF_CHAR * * size of a char */ rb_define_const(rb_mDL, "SIZEOF_CHAR", INT2NUM(sizeof(char))); /* Document-const: SIZEOF_SHORT * * size of a short */ rb_define_const(rb_mDL, "SIZEOF_SHORT", INT2NUM(sizeof(short))); /* Document-const: SIZEOF_INT * * size of an int */ rb_define_const(rb_mDL, "SIZEOF_INT", INT2NUM(sizeof(int))); /* Document-const: SIZEOF_LONG * * size of a long */ rb_define_const(rb_mDL, "SIZEOF_LONG", INT2NUM(sizeof(long))); #if HAVE_LONG_LONG /* Document-const: SIZEOF_LONG_LONG * * size of a long long */ rb_define_const(rb_mDL, "SIZEOF_LONG_LONG", INT2NUM(sizeof(LONG_LONG))); #endif /* Document-const: SIZEOF_FLOAT * * size of a float */ rb_define_const(rb_mDL, "SIZEOF_FLOAT", INT2NUM(sizeof(float))); /* Document-const: SIZEOF_DOUBLE * * size of a double */ rb_define_const(rb_mDL, "SIZEOF_DOUBLE",INT2NUM(sizeof(double))); /* Document-const: SIZEOF_SIZE_T * * size of a size_t */ rb_define_const(rb_mDL, "SIZEOF_SIZE_T", INT2NUM(sizeof(size_t))); /* Document-const: SIZEOF_SSIZE_T * * size of a ssize_t */ rb_define_const(rb_mDL, "SIZEOF_SSIZE_T", INT2NUM(sizeof(size_t))); /* same as size_t */ /* Document-const: SIZEOF_PTRDIFF_T * * size of a ptrdiff_t */ rb_define_const(rb_mDL, "SIZEOF_PTRDIFF_T", INT2NUM(sizeof(ptrdiff_t))); /* Document-const: SIZEOF_INTPTR_T * * size of a intptr_t */ rb_define_const(rb_mDL, "SIZEOF_INTPTR_T", INT2NUM(sizeof(intptr_t))); /* Document-const: SIZEOF_UINTPTR_T * * size of a intptr_t */ rb_define_const(rb_mDL, "SIZEOF_UINTPTR_T", INT2NUM(sizeof(uintptr_t))); rb_define_module_function(rb_mDL, "dlwrap", rb_dl_value2ptr, 1); rb_define_module_function(rb_mDL, "dlunwrap", rb_dl_ptr2value, 1); rb_define_module_function(rb_mDL, "dlopen", rb_dl_dlopen, -1); rb_define_module_function(rb_mDL, "malloc", rb_dl_malloc, 1); rb_define_module_function(rb_mDL, "realloc", rb_dl_realloc, 2); rb_define_module_function(rb_mDL, "free", rb_dl_free, 1); /* Document-const: RUBY_FREE * * Address of the ruby_xfree() function */ rb_define_const(rb_mDL, "RUBY_FREE", PTR2NUM(ruby_xfree)); /* Document-const: BUILD_RUBY_PLATFORM * * Platform built against (i.e. "x86_64-linux", etc.) * * See also RUBY_PLATFORM */ rb_define_const(rb_mDL, "BUILD_RUBY_PLATFORM", rb_str_new2(RUBY_PLATFORM)); /* Document-const: BUILD_RUBY_VERSION * * Ruby Version built. (i.e. "1.9.3") * * See also RUBY_VERSION */ rb_define_const(rb_mDL, "BUILD_RUBY_VERSION", rb_str_new2(RUBY_VERSION)); Init_dlhandle(); Init_dlcfunc(); Init_dlptr(); }