From d10eaa259ba3ff59dc16716a694e92e12a00b103 Mon Sep 17 00:00:00 2001 From: ae2f Date: Thu, 13 Mar 2025 00:53:33 +0900 Subject: [PATCH 01/13] Union based naming --- README.md | 4 +- include/ae2f/Ann/Mlp.h | 97 ++++++++-------------- include/ae2f/Ann/Mlp.h.cxx/Mlp.hh | 6 +- include/ae2f/Ann/Mlp.h.cxx/imp.hh | 10 +-- include/ae2f/Ann/Slp.h | 4 +- include/ae2fCL/Ann/Mlp.h | 8 +- src/ann-h/Mlp.h | 28 +++---- src/ann-h/Mlp/Predict.h | 6 +- src/ann/Mlp.c | 18 ++-- src/cl/Mlp.c | 14 ++-- test/cl-src/MlpTrainXOR.c | 34 ++++---- test/cl-src/SlpTrainAND.c | 2 +- test/src/MlpTrainAND.c | 44 +++++----- test/src/{MlpTrainXOR.c => MlpTrainXOR.cc} | 37 +++++---- 14 files changed, 144 insertions(+), 168 deletions(-) rename test/src/{MlpTrainXOR.c => MlpTrainXOR.cc} (81%) diff --git a/README.md b/README.md index 212396f..20c8f72 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Ann -> Rel-v0.0.0 +> Rel-v1.0.0 ANN interfaces with basic implementation for example. @@ -14,7 +14,7 @@ ANN interfaces with basic implementation for example. - cmake # CL-Ann -> Rel-v2.1.0 +> Rel-v3.1.0 A simple ann project with opencl written in C. diff --git a/include/ae2f/Ann/Mlp.h b/include/ae2f/Ann/Mlp.h index 2077e39..98bc944 100644 --- a/include/ae2f/Ann/Mlp.h +++ b/include/ae2f/Ann/Mlp.h @@ -16,101 +16,76 @@ #include /// @brief -/// Element of @ref ae2f_AnnMlp -typedef ae2f_AnnSlp ae2f_AnnMlpEl; +/// Element of @ref ae2f_mAnnMlp +typedef ae2f_AnnSlp ae2f_mAnnMlpEl; #if ae2f_WhenCXX(!) 0 -struct ae2f_AnnMlp : ae2f_AnnSlp { +struct ae2f_mAnnMlp : private ae2f_AnnSlp { #include "Mlp.h.cxx/Mlp.hh" }; #else -/// @class ae2f_AnnMlp +/// @class ae2f_mAnnMlp /// @extends ae2f_AnnSlp /// @brief /// # Multi Layered Perceptron /// /// -typedef ae2f_AnnSlp ae2f_AnnMlp; +typedef ae2f_AnnSlp ae2f_mAnnMlp; #endif -/// @memberof ae2f_AnnMlp + +typedef union ae2f_AnnMlp { + ae2f_AnnSlp Slp; + ae2f_mAnnMlp Mlp; +} ae2f_AnnMlp; + +/// @memberof ae2f_mAnnMlp /// @details ae2f_AnnSlpPerVPad -#define ae2f_AnnMlpLayerVPad ae2f_AnnSlpPerVPad +#define ae2f_mAnnMlpLayerVPad ae2f_AnnSlpPerVPad -/// @memberof ae2f_AnnMlp -#define ae2f_AnnMlpLayerV(mlp, i, ...) \ -ae2f_reinterpret_cast(__VA_ARGS__ ae2f_AnnMlpEl*, ae2f_AnnMlpLayerVPad(mlp, __VA_ARGS__)[i] + 1) +/// @memberof ae2f_mAnnMlp +#define ae2f_mAnnMlpLayerV(mlp, i, ...) \ +ae2f_reinterpret_cast(__VA_ARGS__ ae2f_mAnnMlpEl*, ae2f_mAnnMlpLayerVPad(mlp, __VA_ARGS__)[i] + 1) -/// @memberof ae2f_AnnMlp -#define ae2f_AnnMlpInitSz(layerc, add) \ -(sizeof(ae2f_AnnMlp) + (sizeof(void*) * ((layerc))) + (sizeof(size_t) * 1) + (add)) +/// @memberof ae2f_mAnnMlp +#define ae2f_mAnnMlpInitSz(layerc, add) \ +(sizeof(ae2f_mAnnMlp) + (sizeof(void*) * ((layerc))) + (sizeof(size_t) * 1) + (add)) /// @brief /// Predict function api. \n -/// For details see @ref ae2f_AnnMlpPredict_t. -typedef ae2f_AnnSlpPredict_t ae2f_AnnMlpPredict_t; +/// For details see @ref ae2f_mAnnMlpPredict_t. +typedef ae2f_AnnSlpPredict_t ae2f_mAnnMlpPredict_t; /// @brief /// Training function api. \n -/// For details see @ref ae2f_AnnMlpTrain_t -typedef ae2f_AnnSlpTrain_t ae2f_AnnMlpTrain_t; +/// For details see @ref ae2f_mAnnMlpTrain_t +typedef ae2f_AnnSlpTrain_t ae2f_mAnnMlpTrain_t; /// @brief /// Cleaning function api. -typedef ae2f_AnnSlpClean_t ae2f_AnnMlpClean_t; +typedef ae2f_AnnSlpClean_t ae2f_mAnnMlpClean_t; -/// @memberof ae2f_AnnMlp +/// @memberof ae2f_mAnnMlp /// @brief /// The predicted max buffer count among all perceptron's possible length of I/O. -#define ae2f_AnnMlpLayerBuffCount(mlp, ...) \ +#define ae2f_mAnnMlpLayerBuffCount(mlp, ...) \ ae2f_AnnSlpX(mlp, __VA_ARGS__ size_t*, __VA_ARGS__) -/// @memberof ae2f_AnnMlp -/// @brief -/// Its length is @ref (*ae2f_AnnMlpLayerBuffCount(mlp) * mlp->layerc, 3 * sizeof(ae2f_float_t)). -#define ae2f_AnnMlpCache(mlp, ...) \ -ae2f_reinterpret_cast(__VA_ARGS__ ae2f_float_t**, ae2f_AnnMlpLayerBuffCount(mlp, __VA_ARGS__) + 1) - -/// @memberof ae2f_AnnMlp -/// @brief -#define ae2f_AnnMlpX(mlp,type,...) \ -ae2f_reinterpret_cast(__VA_ARGS__ type, ae2f_AnnMlpCache(mlp, __VA_ARGS__) + 1) - -/// @memberof ae2f_AnnMlp +/// @memberof ae2f_mAnnMlp /// @brief -/// Predict function call. \n -/// See @ref ae2f_AnnSlpPredict -#define ae2f_AnnMlpPredict ae2f_AnnSlpPredict +/// Its length is @ref (*ae2f_mAnnMlpLayerBuffCount(mlp) * mlp->layerc, 3 * sizeof(ae2f_float_t)). +#define ae2f_mAnnMlpCache(mlp, ...) \ +ae2f_reinterpret_cast(__VA_ARGS__ ae2f_float_t**, ae2f_mAnnMlpLayerBuffCount(mlp, __VA_ARGS__) + 1) -/// @memberof ae2f_AnnMlp +/// @memberof ae2f_mAnnMlp /// @brief -/// Training function call. \n -/// See @ref ae2f_AnnSlpTrain -#define ae2f_AnnMlpTrain ae2f_AnnSlpTrain +#define ae2f_mAnnMlpX(mlp,type,...) \ +ae2f_reinterpret_cast(__VA_ARGS__ type, ae2f_mAnnMlpCache(mlp, __VA_ARGS__) + 1) -/// @memberof ae2f_AnnMlp -/// @brief -/// See @ref ae2f_AnnSlpTrainA -#define ae2f_AnnMlpTrainA ae2f_AnnSlpTrainA - -/// @memberof ae2f_AnnMlp -/// @brief -/// See @ref ae2f_AnnSlpTrainB -#define ae2f_AnnMlpTrainB ae2f_AnnSlpTrainB - -/// @memberof ae2f_AnnMlp -/// @brief -/// Deleting function. \n -/// We are reusing the previous definition since the process is literally saem. -/// -/// See @ref ae2f_AnnSlpDel -#define ae2f_AnnMlpDel ae2f_AnnSlpDel -#define ae2f_AnnMlpClean ae2f_AnnSlpClean - -/// @memberof ae2f_AnnMlp +/// @memberof ae2f_mAnnMlp /// @brief /// d /// @param _this @@ -125,8 +100,8 @@ ae2f_reinterpret_cast(__VA_ARGS__ type, ae2f_AnnMlpCache(mlp, __VA_ARGS__) + 1) /// @param errret_opt /// @return ae2f_extern ae2f_SHAREDCALL -size_t ae2f_AnnMlpInit( - ae2f_AnnMlp* _this, +size_t ae2f_mAnnMlpInit( + ae2f_mAnnMlp* _this, size_t layerc, size_t add_opt, const size_t* layerlenv, diff --git a/include/ae2f/Ann/Mlp.h.cxx/Mlp.hh b/include/ae2f/Ann/Mlp.h.cxx/Mlp.hh index 5925d98..47a1225 100644 --- a/include/ae2f/Ann/Mlp.h.cxx/Mlp.hh +++ b/include/ae2f/Ann/Mlp.h.cxx/Mlp.hh @@ -7,13 +7,13 @@ * * @copyright Copyright (c) 2025 * - * Additional definition for @ref ae2f_AnnMlp, @ref ae2f_AnnMlpEl + * Additional definition for @ref ae2f_mAnnMlp, @ref ae2f_mAnnMlpEl * */ #include "../Mlp.h" #define ae2f_TMP -constexprfun ae2f_AnnMlpEl* ae2f_TMP Perc(size_t a); -constexprfun const ae2f_AnnMlpEl* ae2f_TMP Perc(size_t a) const; +constexprfun ae2f_mAnnMlpEl* ae2f_TMP Perc(size_t a); +constexprfun const ae2f_mAnnMlpEl* ae2f_TMP Perc(size_t a) const; #undef ae2f_TMP \ No newline at end of file diff --git a/include/ae2f/Ann/Mlp.h.cxx/imp.hh b/include/ae2f/Ann/Mlp.h.cxx/imp.hh index 2f63292..753ceab 100644 --- a/include/ae2f/Ann/Mlp.h.cxx/imp.hh +++ b/include/ae2f/Ann/Mlp.h.cxx/imp.hh @@ -11,13 +11,13 @@ * */ -#define ae2f_TMP ae2f_AnnMlp:: +#define ae2f_TMP ae2f_mAnnMlp:: -constexprfun ae2f_AnnMlpEl* ae2f_TMP Perc(size_t a) { - return ae2f_AnnMlpLayerV(this, a); +constexprfun ae2f_mAnnMlpEl* ae2f_TMP Perc(size_t a) { + return ae2f_mAnnMlpLayerV(this, a); } -constexprfun const ae2f_AnnMlpEl* ae2f_TMP Perc(size_t a) const { - return ae2f_AnnMlpLayerV(this, a, const); +constexprfun const ae2f_mAnnMlpEl* ae2f_TMP Perc(size_t a) const { + return ae2f_mAnnMlpLayerV(this, a, const); } #undef ae2f_TMP \ No newline at end of file diff --git a/include/ae2f/Ann/Slp.h b/include/ae2f/Ann/Slp.h index a8c164d..fedf98b 100644 --- a/include/ae2f/Ann/Slp.h +++ b/include/ae2f/Ann/Slp.h @@ -95,7 +95,7 @@ typedef struct ae2f_AnnSlp { /// Training function ae2f_AnnSlpTrain_t* vTrain; - #if ae2f_WhenCXX(1) + 0 + #if ae2f_WhenCXX(!)0 #include "Slp.h.cxx/Slp.hh" #endif } ae2f_AnnSlp; @@ -294,7 +294,7 @@ ae2f_AnnDelDef(ae2f_AnnSlp) noexcept { #include "Sp.h.c/clean.script.h" } -#if ae2f_WhenCXX(1) + 0 +#if ae2f_WhenCXX(!)0 #include "Slp.h.cxx/imp.hh" #endif diff --git a/include/ae2fCL/Ann/Mlp.h b/include/ae2fCL/Ann/Mlp.h index ba0d678..70c5863 100644 --- a/include/ae2fCL/Ann/Mlp.h +++ b/include/ae2fCL/Ann/Mlp.h @@ -6,7 +6,7 @@ ae2f_extern ae2f_SHAREDCALL size_t ae2fCL_AnnMlpInit( - ae2f_AnnMlp* _this, + ae2f_mAnnMlp* _this, size_t layerc, size_t add_opt, const size_t* layerlenv, @@ -33,8 +33,8 @@ ae2f_AnnMlp* ae2fCL_AnnMlpMk( cl_int* errnfound_opt ) noexcept; -#define ae2fCL_AnnMlpInitSz ae2f_AnnMlpInitSz -#define ae2fCL_AnnMlpDel ae2f_AnnMlpDel -#define ae2fCL_AnnMlpClean ae2f_AnnMlpClean +#define ae2fCL_AnnMlpInitSz ae2f_mAnnMlpInitSz +#define ae2fCL_AnnMlpDel ae2f_mAnnMlpDel +#define ae2fCL_AnnMlpClean ae2f_mAnnMlpClean #endif \ No newline at end of file diff --git a/src/ann-h/Mlp.h b/src/ann-h/Mlp.h index cb98d7d..7b93264 100644 --- a/src/ann-h/Mlp.h +++ b/src/ann-h/Mlp.h @@ -18,9 +18,9 @@ #include "Mlp/Predict.h" #include -static ae2f_AnnMlpPredict_t Predict; -static ae2f_AnnMlpTrain_t Train; -static ae2f_AnnMlpClean_t Clean; +static ae2f_mAnnMlpPredict_t Predict; +static ae2f_mAnnMlpTrain_t Train; +static ae2f_mAnnMlpClean_t Clean; static ae2f_err_t Predict ( const ae2f_AnnSlp* _this, @@ -31,7 +31,7 @@ static ae2f_err_t Predict ( if(!in) return ae2f_errGlob_PTR_IS_NULL; if(!outret_opt) return ae2f_errGlob_DONE_HOWEV | ae2f_errGlob_PTR_IS_NULL; - size_t A = *ae2f_AnnMlpLayerBuffCount(_this, const); + size_t A = *ae2f_mAnnMlpLayerBuffCount(_this, const); union { const ae2f_float_t* CF; @@ -39,14 +39,14 @@ static ae2f_err_t Predict ( uintptr_t P; } tmpVi = { in }, - tmpVo = { *ae2f_AnnMlpCache(_this, const) }; + tmpVo = { *ae2f_mAnnMlpCache(_this, const) }; if(!tmpVo.P) return ae2f_errGlob_ALLOC_FAILED | ae2f_errGlob_PTR_IS_NULL; ae2f_err_t err = 0; for(size_t i = 0; i < _this->layerc; i++) { - const ae2f_AnnSlp* slp = ae2f_AnnMlpLayerV(_this, i, const); + const ae2f_AnnSlp* slp = ae2f_mAnnMlpLayerV(_this, i, const); if(i == _this->layerc - 1) tmpVo.F = outret_opt; @@ -71,13 +71,13 @@ static ae2f_err_t Train ( ae2f_err_t ret = 0; #define return(n) { ret |= n; goto RET; } const size_t - MAXBUFFCOUNT_FOR_LAYER = *ae2f_AnnMlpLayerBuffCount(_this), + MAXBUFFCOUNT_FOR_LAYER = *ae2f_mAnnMlpLayerBuffCount(_this), SkipInput = MAXBUFFCOUNT_FOR_LAYER * _this->layerc; #define _cache_ALLOCCOUNT SkipInput * 3 #define _cache_SIZE sizeof(ae2f_float_t) - ae2f_float_t* const _cache = *ae2f_AnnMlpCache(_this); + ae2f_float_t* const _cache = *ae2f_mAnnMlpCache(_this); #define _cache_Out _cache if(!_cache) return(ae2f_errGlob_ALLOC_FAILED); @@ -93,8 +93,8 @@ static ae2f_err_t Train ( for(size_t i = _this->layerc - 1; i != ((size_t)-1); i--) { ae2f_AnnSlp - * const LAYER = ae2f_AnnMlpLayerV(_this, i), - * const LAYERNXT = ae2f_AnnMlpLayerV(_this, i + 1); + * const LAYER = ae2f_mAnnMlpLayerV(_this, i), + * const LAYERNXT = ae2f_mAnnMlpLayerV(_this, i + 1); if(i == _this->layerc - 1) { if (delta_optA) @@ -136,7 +136,7 @@ static ae2f_err_t Train ( } static ae2f_err_t Clean( - ae2f_AnnMlp* _this + ae2f_mAnnMlp* _this ) noexcept { if(!_this) return ae2f_errGlob_PTR_IS_NULL; @@ -148,14 +148,14 @@ static ae2f_err_t Clean( size_t* pad; ae2f_AnnSlp* slp; }* u; - } perc = { .unused = ae2f_AnnMlpLayerVPad(_this) + i }; + } perc = { .unused = ae2f_mAnnMlpLayerVPad(_this) + i }; perc.u->pad++; ae2f_AnnSlpClean(perc.u->slp); free(--perc.u->pad); - ae2f_AnnMlpLayerVPad(_this, )[i] = 0; + ae2f_mAnnMlpLayerVPad(_this, )[i] = 0; } - if(*ae2f_AnnMlpCache(_this)) free(*ae2f_AnnMlpCache(_this)); + if(*ae2f_mAnnMlpCache(_this)) free(*ae2f_mAnnMlpCache(_this)); _this->inc = 0; _this->outc = 0; diff --git a/src/ann-h/Mlp/Predict.h b/src/ann-h/Mlp/Predict.h index 7ee2be1..6ec9adf 100644 --- a/src/ann-h/Mlp/Predict.h +++ b/src/ann-h/Mlp/Predict.h @@ -1,14 +1,14 @@ #include #include static ae2f_err_t MlpTrain_Predict( - const ae2f_AnnMlp* _this, + const ae2f_mAnnMlp* _this, const ae2f_float_t* in, ae2f_float_t* outcache ) { if(!_this) return ae2f_errGlob_PTR_IS_NULL; if(!in) return ae2f_errGlob_PTR_IS_NULL; - size_t A = *ae2f_AnnMlpLayerBuffCount(_this, const); + size_t A = *ae2f_mAnnMlpLayerBuffCount(_this, const); union { const ae2f_float_t* CF; @@ -23,7 +23,7 @@ static ae2f_err_t MlpTrain_Predict( ae2f_err_t err = 0; for(size_t i = 0; i < _this->layerc; i++) { - const ae2f_AnnSlp* slp = ae2f_AnnMlpLayerV(_this, i, const); + const ae2f_AnnSlp* slp = ae2f_mAnnMlpLayerV(_this, i, const); err |= ae2f_AnnSlpPredict( slp, diff --git a/src/ann/Mlp.c b/src/ann/Mlp.c index 42241b7..0f1b58f 100644 --- a/src/ann/Mlp.c +++ b/src/ann/Mlp.c @@ -1,8 +1,8 @@ #include ae2f_SHAREDEXPORT -size_t ae2f_AnnMlpInit( - ae2f_AnnMlp* _this, +size_t ae2f_mAnnMlpInit( + ae2f_mAnnMlp* _this, size_t layerc, size_t add_opt, const size_t* layerlenv, @@ -46,7 +46,7 @@ size_t ae2f_AnnMlpInit( ae2f_AnnSlp* slp; }* u; } perc = { - ae2f_AnnMlpLayerVPad(_this) + i + ae2f_mAnnMlpLayerVPad(_this) + i }; perc.u->pad = calloc( @@ -69,16 +69,16 @@ size_t ae2f_AnnMlpInit( err = err | e & ~ae2f_errGlob_DONE_HOWEV; } - ae2f_AnnMlpCache(_this,)[0] = calloc( + ae2f_mAnnMlpCache(_this,)[0] = calloc( (max * layerc) << 2, sizeof(ae2f_float_t) ); - *ae2f_AnnMlpLayerBuffCount(_this) = max; + *ae2f_mAnnMlpLayerBuffCount(_this) = max; EXIT: #undef return if(errret_opt) *errret_opt = err; - return ae2f_AnnMlpInitSz(++layerc, add_opt); + return ae2f_mAnnMlpInitSz(++layerc, add_opt); } ae2f_SHAREDEXPORT @@ -93,9 +93,9 @@ ae2f_AnnMlp* ae2f_AnnMlpMk( const ae2f_float_t* weights_opt, ae2f_err_t* errret_opt ) noexcept { - ae2f_AnnMlp* obj = calloc(ae2f_AnnMlpInitSz(layerc, add_opt), 1); - ae2f_AnnMlpInit( - obj, layerc, add_opt, layerlenv, + ae2f_AnnMlp* obj = calloc(ae2f_mAnnMlpInitSz(layerc, add_opt), 1); + ae2f_mAnnMlpInit( + &obj->Mlp, layerc, add_opt, layerlenv, layerpadv_opt, inpadv_opt, actglob_opt, deltaglob_opt, weights_opt, errret_opt ); diff --git a/src/cl/Mlp.c b/src/cl/Mlp.c index 7d9b2db..ec21b2f 100644 --- a/src/cl/Mlp.c +++ b/src/cl/Mlp.c @@ -4,7 +4,7 @@ ae2f_SHAREDEXPORT size_t ae2fCL_AnnMlpInit( - ae2f_AnnMlp* _this, + ae2f_mAnnMlp* _this, size_t layerc, size_t add_opt, const size_t* layerlenv, @@ -51,7 +51,7 @@ size_t ae2fCL_AnnMlpInit( size_t* pad; ae2f_AnnSlp* slp; }* u; - } perc = {ae2f_AnnMlpLayerVPad(_this) + i}; + } perc = {ae2f_mAnnMlpLayerVPad(_this) + i}; perc.u->pad = calloc(ae2fCL_AnnSlpInitSz(LAYERSZ_R, sizeof(size_t)), 1); perc.u->pad++; @@ -65,11 +65,11 @@ size_t ae2fCL_AnnMlpInit( err = err | e & ~ae2f_errGlob_DONE_HOWEV; } - ae2f_AnnMlpCache(_this,)[0] = calloc( + ae2f_mAnnMlpCache(_this,)[0] = calloc( (max * layerc) << 2, sizeof(ae2f_float_t) ); - *ae2f_AnnMlpLayerBuffCount(_this) = max; + *ae2f_mAnnMlpLayerBuffCount(_this) = max; EXIT: #undef return @@ -77,7 +77,7 @@ size_t ae2fCL_AnnMlpInit( *errret_opt = err | (err2 ? ae2f_errGlob_NFOUND : 0); } if(errnfound_opt) *errnfound_opt= err2; - return ae2f_AnnMlpInitSz(++layerc, add_opt); + return ae2f_mAnnMlpInitSz(++layerc, add_opt); } ae2f_extern ae2f_SHAREDCALL @@ -95,10 +95,10 @@ ae2f_AnnMlp* ae2fCL_AnnMlpMk( ) noexcept { ae2f_err_t err = 0; cl_int err2 = 0; - ae2f_AnnMlp* obj = calloc(ae2f_AnnMlpInitSz(layerc, add_opt), 1); + ae2f_AnnMlp* obj = calloc(ae2f_mAnnMlpInitSz(layerc, add_opt), 1); ae2fCL_AnnMlpInit( - obj, layerc, add_opt, + &obj->Mlp, layerc, add_opt, layerlenv, layerpadv_opt, inpadv_opt, actglob_opt, deltaglob_opt, weights_opt, diff --git a/test/cl-src/MlpTrainXOR.c b/test/cl-src/MlpTrainXOR.c index 5be0b42..7e2e8d7 100644 --- a/test/cl-src/MlpTrainXOR.c +++ b/test/cl-src/MlpTrainXOR.c @@ -71,32 +71,32 @@ int main() { goto __failure; } for(size_t _ = 0; _ < gEpochs; _++) { - err2 = ae2f_AnnMlpTrainB( - Mlp, ins, + err2 = ae2f_AnnSlpTrainB( + &Mlp->Slp, ins, goals + 2, gLearningRate ); if(err2) { err_ae2f = err2; goto __failure; } - err2 = ae2f_AnnMlpTrainB( - Mlp, ins + 2, + err2 = ae2f_AnnSlpTrainB( + &Mlp->Slp, ins + 2, goals, gLearningRate ); if(err2) { goto __failure; } - err2 = ae2f_AnnMlpTrainB( - Mlp, ins + 4, + err2 = ae2f_AnnSlpTrainB( + &Mlp->Slp, ins + 4, goals, gLearningRate ); if(err2) { goto __failure; } - err2 = ae2f_AnnMlpTrainB( - Mlp, ins + 6, + err2 = ae2f_AnnSlpTrainB( + &Mlp->Slp, ins + 6, goals + 2, gLearningRate ); if(err2) { @@ -108,8 +108,8 @@ int main() { #if 1 puts("Predict time"); - err2 = ae2f_AnnMlpPredict( - Mlp, ins, outbuff + err2 = ae2f_AnnSlpPredict( + &Mlp->Slp, ins, outbuff ); if(err2) { goto __failure; } printf("Checking the value: %f\n", outbuff[0]); @@ -118,8 +118,8 @@ int main() { err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnMlpPredict( - Mlp, ins + 6, outbuff + err2 = ae2f_AnnSlpPredict( + &Mlp->Slp, ins + 6, outbuff ); if(err2) { err2 = err2; goto __failure; } printf("Checking the value: %f\n", outbuff[0]); @@ -128,8 +128,8 @@ int main() { err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnMlpPredict( - Mlp, ins + 4, outbuff + err2 = ae2f_AnnSlpPredict( + &Mlp->Slp, ins + 4, outbuff ); if(err2) { err_ae2f = err2; goto __failure; } printf("Checking the value: %f\n", outbuff[0]); @@ -138,8 +138,8 @@ int main() { err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnMlpPredict( - Mlp, ins + 2, outbuff + err2 = ae2f_AnnSlpPredict( + &Mlp->Slp, ins + 2, outbuff ); if(err2) { err_ae2f = err2; goto __failure; } printf("Checking the value: %f\n", outbuff[0]); @@ -151,7 +151,7 @@ int main() { #endif __failure: - ae2f_AnnMlpDel(Mlp); + ae2f_AnnSlpDel(&Mlp->Slp); ae2fCL_AnnDel(); printf("Something is over, code: %d\n", err_ae2f | err2); return err_ae2f | err2; diff --git a/test/cl-src/SlpTrainAND.c b/test/cl-src/SlpTrainAND.c index fc82529..21cdfae 100644 --- a/test/cl-src/SlpTrainAND.c +++ b/test/cl-src/SlpTrainAND.c @@ -4,7 +4,7 @@ #include #define gLearningRate 0.1 -#define gEpochs 9000 +#define gEpochs 5000 static ae2f_float_t Forward(ae2f_float_t x) { diff --git a/test/src/MlpTrainAND.c b/test/src/MlpTrainAND.c index a18f3e5..26320a5 100644 --- a/test/src/MlpTrainAND.c +++ b/test/src/MlpTrainAND.c @@ -61,32 +61,32 @@ int main() { goto __failure; } for(size_t _ = 0; _ < gEpochs; _++) { - err2 = ae2f_AnnMlpTrainB( - Mlp, ins, + err2 = ae2f_AnnSlpTrainB( + &Mlp->Slp, ins, goals + 0, gLearningRate ); if(err2) { err_ae2f = err2; goto __failure; } - err2 = ae2f_AnnMlpTrainB( - Mlp, ins + 2, + err2 = ae2f_AnnSlpTrainB( + &Mlp->Slp, ins + 2, goals + 2, gLearningRate ); if(err2) { goto __failure; } - err2 = ae2f_AnnMlpTrainB( - Mlp, ins + 4, + err2 = ae2f_AnnSlpTrainB( + &Mlp->Slp, ins + 4, goals + 2, gLearningRate ); if(err2) { goto __failure; } - err2 = ae2f_AnnMlpTrainB( - Mlp, ins + 6, + err2 = ae2f_AnnSlpTrainB( + &Mlp->Slp, ins + 6, goals + 2, gLearningRate ); if(err2) { @@ -98,8 +98,8 @@ int main() { #if 1 puts("Predict time"); - err2 = ae2f_AnnMlpPredict( - Mlp, ins, outbuff + err2 = ae2f_AnnSlpPredict( + &Mlp->Mlp, ins, outbuff ); if(err2) { goto __failure; } @@ -109,12 +109,12 @@ int main() { ae2f_float_t tmp[3] = {}; err2 = ae2f_AnnSlpPredict( - ae2f_AnnMlpLayerV(Mlp, 0), + ae2f_mAnnMlpLayerV(&Mlp->Mlp, 0), ins, tmp ); err2 = ae2f_AnnSlpPredict( - ae2f_AnnMlpLayerV(Mlp, 1), + ae2f_mAnnMlpLayerV(&Mlp->Mlp, 1), tmp, outbuff ); printf("Checking the value(SLP): %f\n", outbuff[0]); @@ -122,8 +122,8 @@ int main() { err2 = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnMlpPredict( - Mlp, ins + 6, outbuff + err2 = ae2f_AnnSlpPredict( + &Mlp->Slp, ins + 6, outbuff ); if(err2) { err2 = err2; goto __failure; } printf("Checking the value: %f\n", outbuff[0]); @@ -131,7 +131,7 @@ int main() { printf("AND 0, 0 no good\n"); err2 = ae2f_AnnSlpPredict( - ae2f_AnnMlpLayerV(Mlp, 0), + ae2f_mAnnMlpLayerV(&Mlp->Mlp, 0), ins, outbuff ); printf("Checking the value(SLP): %f\n", outbuff[0]); @@ -139,30 +139,30 @@ int main() { err2 = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnMlpPredict( - Mlp, ins + 4, outbuff + err2 = ae2f_AnnSlpPredict( + &Mlp->Slp, ins + 4, outbuff ); if(err2) { err_ae2f = err2; goto __failure; } printf("Checking the value: %f\n", outbuff[0]); if(outbuff[0] > 0.5) { printf("AND 0, 1 no good\n"); err2 = ae2f_AnnSlpPredict( - ae2f_AnnMlpLayerV(Mlp, 0), + ae2f_mAnnMlpLayerV(&Mlp->Mlp, 0), ins, outbuff ); printf("Checking the value(SLP): %f\n", outbuff[0]); err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnMlpPredict( - Mlp, ins + 2, outbuff + err2 = ae2f_AnnSlpPredict( + &Mlp->Mlp, ins + 2, outbuff ); if(err2) { err_ae2f = err2; goto __failure; } printf("Checking the value: %f\n", outbuff[0]); if(outbuff[0] > 0.5) { printf("AND 1, 0 no good\n"); err2 = ae2f_AnnSlpPredict( - ae2f_AnnMlpLayerV(Mlp, 0), + ae2f_mAnnMlpLayerV(&Mlp->Mlp, 0), ins, outbuff ); printf("Checking the value(SLP): %f\n", outbuff[0]); @@ -172,7 +172,7 @@ int main() { #endif __failure: - ae2f_AnnMlpDel(Mlp); + ae2f_AnnSlpDel(&Mlp->Slp); printf("Something is over, code: %d\n", err_ae2f); return err_ae2f | err2; } \ No newline at end of file diff --git a/test/src/MlpTrainXOR.c b/test/src/MlpTrainXOR.cc similarity index 81% rename from test/src/MlpTrainXOR.c rename to test/src/MlpTrainXOR.cc index 0488ebe..3cc2f64 100644 --- a/test/src/MlpTrainXOR.c +++ b/test/src/MlpTrainXOR.cc @@ -37,6 +37,7 @@ int main() { ae2f_err_t err_ae2f = 0; ae2f_err_t err2 = 0; ae2f_float_t diff_got[2] = {0, }; + ae2f_AnnMlp* Mlp = 0; size_t sizes[] = {2, 3, 1}; ae2f_float_t outbuff[2] = { 0, 0 }; @@ -62,32 +63,32 @@ int main() { goto __failure; } for(size_t _ = 0; _ < gEpochs; _++) { - err2 = ae2f_AnnMlpTrainB( - Mlp, ins, - goals + 2, gLearningRate + err2 = ae2f_AnnSlpTrainB( + &Mlp->Slp, + ins, goals + 2, gLearningRate ); if(err2) { err_ae2f = err2; goto __failure; } - err2 = ae2f_AnnMlpTrainB( - Mlp, ins + 2, + err2 = ae2f_AnnSlpTrainB( + &Mlp->Slp, ins + 2, goals, gLearningRate ); if(err2) { goto __failure; } - err2 = ae2f_AnnMlpTrainB( - Mlp, ins + 4, + err2 = ae2f_AnnSlpTrainB( + &Mlp->Slp, ins + 4, goals, gLearningRate ); if(err2) { goto __failure; } - err2 = ae2f_AnnMlpTrainB( - Mlp, ins + 6, + err2 = ae2f_AnnSlpTrainB( + &Mlp->Slp, ins + 6, goals + 2, gLearningRate ); if(err2) { @@ -99,8 +100,8 @@ int main() { #if 1 puts("Predict time"); - err2 = ae2f_AnnMlpPredict( - Mlp, ins, outbuff + err2 = ae2f_AnnSlpPredict( + &Mlp->Slp, ins, outbuff ); if(err2) { goto __failure; } printf("Checking the value: %f\n", outbuff[0]); @@ -109,8 +110,8 @@ int main() { err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnMlpPredict( - Mlp, ins + 6, outbuff + err2 = ae2f_AnnSlpPredict( + &Mlp->Slp, ins + 6, outbuff ); if(err2) { err2 = err2; goto __failure; } printf("Checking the value: %f\n", outbuff[0]); @@ -119,8 +120,8 @@ int main() { err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnMlpPredict( - Mlp, ins + 4, outbuff + err2 = ae2f_AnnSlpPredict( + &Mlp->Slp, ins + 4, outbuff ); if(err2) { err_ae2f = err2; goto __failure; } printf("Checking the value: %f\n", outbuff[0]); @@ -129,8 +130,8 @@ int main() { err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnMlpPredict( - Mlp, ins + 2, outbuff + err2 = ae2f_AnnSlpPredict( + &Mlp->Slp, ins + 2, outbuff ); if(err2) { err_ae2f = err2; goto __failure; } printf("Checking the value: %f\n", outbuff[0]); @@ -142,7 +143,7 @@ int main() { #endif __failure: - ae2f_AnnMlpDel(Mlp); + ae2f_AnnSlpDel(&Mlp->Slp); printf("Something is over, code: %d\n", err_ae2f | err2); return err_ae2f | err2; } \ No newline at end of file From 7138622149f523e61f1d0a4d5b4283871f4f08f3 Mon Sep 17 00:00:00 2001 From: ae2f Date: Thu, 13 Mar 2025 02:15:56 +0900 Subject: [PATCH 02/13] CPU Refactor #1 --- include/ae2f/Ann/Mlp.h | 11 +- include/ae2f/Ann/Mlp.h.cxx/Mlp.hh | 22 +-- include/ae2f/Ann/Mlp.h.cxx/imp.hh | 11 +- include/ae2f/Ann/Mlp.h.cxx/mMlp.hh | 19 +++ include/ae2f/Ann/Slp.h | 34 ++--- include/ae2f/Ann/Slp.h.cxx/Slp.hh | 6 +- include/ae2f/Ann/Slp.h.cxx/imp.hh | 6 +- include/ae2f/Ann/Sp.h | 120 ++++++++------- include/ae2f/Ann/Sp.h.cxx/Sp.hh | 44 +----- include/ae2f/Ann/Sp.h.cxx/imp.hh | 38 +++-- include/ae2f/Ann/Sp.h.cxx/mSp.hh | 49 ++++++ include/ae2fCL/Ann/Sp.h | 28 ++-- src/ann-h/Mlp/HidErr.h | 4 +- src/ann-h/Slp.h | 16 +- src/ann-h/Sp.h | 22 +-- src/ann/Slp.c | 4 +- src/ann/Sp.c | 18 +-- src/cl/Slp.c | 28 ++-- src/cl/Sp.c | 22 +-- test/cl-src/PercMk.cc | 6 +- test/cl-src/SlpMk.cc | 12 +- test/cl-src/SlpTrainAND.c | 2 +- test/cl-src/SpTrainAND.c | 10 +- test/src/MlpTrainXOR.cc | 119 ++++++++++++++- test/src/PercMk.cc | 28 ++-- test/src/{SlpTrainAND.c => SlpTrainAND.cc} | 169 ++++++++++++++++++++- test/src/SpTrainAND.c | 10 +- 27 files changed, 592 insertions(+), 266 deletions(-) create mode 100644 include/ae2f/Ann/Mlp.h.cxx/mMlp.hh create mode 100644 include/ae2f/Ann/Sp.h.cxx/mSp.hh rename test/src/{SlpTrainAND.c => SlpTrainAND.cc} (50%) diff --git a/include/ae2f/Ann/Mlp.h b/include/ae2f/Ann/Mlp.h index 98bc944..3038d81 100644 --- a/include/ae2f/Ann/Mlp.h +++ b/include/ae2f/Ann/Mlp.h @@ -21,8 +21,11 @@ typedef ae2f_AnnSlp ae2f_mAnnMlpEl; #if ae2f_WhenCXX(!) 0 -struct ae2f_mAnnMlp : private ae2f_AnnSlp { -#include "Mlp.h.cxx/Mlp.hh" +struct ae2f_mAnnMlp { +#include "Mlp.h.cxx/mMlp.hh" + +private: +ae2f_AnnSlp _this[1]; }; #else @@ -40,6 +43,10 @@ typedef ae2f_AnnSlp ae2f_mAnnMlp; typedef union ae2f_AnnMlp { ae2f_AnnSlp Slp; ae2f_mAnnMlp Mlp; + + #if ae2f_WhenCXX(!)0 + #include "Mlp.h.cxx/Mlp.hh" + #endif } ae2f_AnnMlp; /// @memberof ae2f_mAnnMlp diff --git a/include/ae2f/Ann/Mlp.h.cxx/Mlp.hh b/include/ae2f/Ann/Mlp.h.cxx/Mlp.hh index 47a1225..237c47c 100644 --- a/include/ae2f/Ann/Mlp.h.cxx/Mlp.hh +++ b/include/ae2f/Ann/Mlp.h.cxx/Mlp.hh @@ -1,19 +1,9 @@ -/** - * @file Mlp.hh - * @author ae2f - * @brief - * @version 0.1 - * @date 2025-02-19 - * - * @copyright Copyright (c) 2025 - * - * Additional definition for @ref ae2f_mAnnMlp, @ref ae2f_mAnnMlpEl - * - */ - #include "../Mlp.h" #define ae2f_TMP -constexprfun ae2f_mAnnMlpEl* ae2f_TMP Perc(size_t a); -constexprfun const ae2f_mAnnMlpEl* ae2f_TMP Perc(size_t a) const; -#undef ae2f_TMP \ No newline at end of file +inline ae2f_TMP ~ae2f_AnnMlp(); +#undef ae2f_TMP + +private: +uint8_t szero; +constexpr ae2f_AnnMlp() : szero(0) {} \ No newline at end of file diff --git a/include/ae2f/Ann/Mlp.h.cxx/imp.hh b/include/ae2f/Ann/Mlp.h.cxx/imp.hh index 753ceab..973da5b 100644 --- a/include/ae2f/Ann/Mlp.h.cxx/imp.hh +++ b/include/ae2f/Ann/Mlp.h.cxx/imp.hh @@ -14,10 +14,17 @@ #define ae2f_TMP ae2f_mAnnMlp:: constexprfun ae2f_mAnnMlpEl* ae2f_TMP Perc(size_t a) { - return ae2f_mAnnMlpLayerV(this, a); + return ae2f_mAnnMlpLayerV(this->_this, a); } constexprfun const ae2f_mAnnMlpEl* ae2f_TMP Perc(size_t a) const { - return ae2f_mAnnMlpLayerV(this, a, const); + return ae2f_mAnnMlpLayerV(this->_this, a, const); } +#undef ae2f_TMP + + +#define ae2f_TMP ae2f_AnnMlp:: +inline ae2f_TMP ~ae2f_AnnMlp() { + ae2f_AnnSlpClean(&this->Slp); +} #undef ae2f_TMP \ No newline at end of file diff --git a/include/ae2f/Ann/Mlp.h.cxx/mMlp.hh b/include/ae2f/Ann/Mlp.h.cxx/mMlp.hh new file mode 100644 index 0000000..47a1225 --- /dev/null +++ b/include/ae2f/Ann/Mlp.h.cxx/mMlp.hh @@ -0,0 +1,19 @@ +/** + * @file Mlp.hh + * @author ae2f + * @brief + * @version 0.1 + * @date 2025-02-19 + * + * @copyright Copyright (c) 2025 + * + * Additional definition for @ref ae2f_mAnnMlp, @ref ae2f_mAnnMlpEl + * + */ + +#include "../Mlp.h" + +#define ae2f_TMP +constexprfun ae2f_mAnnMlpEl* ae2f_TMP Perc(size_t a); +constexprfun const ae2f_mAnnMlpEl* ae2f_TMP Perc(size_t a) const; +#undef ae2f_TMP \ No newline at end of file diff --git a/include/ae2f/Ann/Slp.h b/include/ae2f/Ann/Slp.h index fedf98b..187c874 100644 --- a/include/ae2f/Ann/Slp.h +++ b/include/ae2f/Ann/Slp.h @@ -24,10 +24,10 @@ typedef ae2f_err_t ae2f_AnnSlpClean_t (ae2f_struct ae2f_AnnSlp*) noexcept; /// Predict one output from multiple inputs. /// @param[in] in /// Input vector. \n -/// Its count is @ref ae2f_AnnSp::inc. +/// Its count is @ref ae2f_mAnnSp::inc. /// @param[out] outret_opt /// That one predicted. \n -/// Its count is @ref ae2f_AnnSp::inc. +/// Its count is @ref ae2f_mAnnSp::inc. /// @return State typedef ae2f_err_t ae2f_AnnSlpPredict_t ( const ae2f_struct ae2f_AnnSlp* _this, @@ -39,7 +39,7 @@ typedef ae2f_err_t ae2f_AnnSlpPredict_t ( /// Training function, multiple input from multiple output. /// @param[in] in /// Input vector. \n -/// Its count is @ref ae2f_AnnSp::inc. +/// Its count is @ref ae2f_mAnnSp::inc. /// @param[in] delta_optA /// Delta vector when you pre-calculated it. /// Its count is one. @@ -57,7 +57,7 @@ typedef ae2f_err_t ae2f_AnnSlpTrain_t ( /// @brief /// The element type for @ref ae2f_AnnSlp. -typedef struct ae2f_AnnSp ae2f_AnnSlpEl; +typedef struct ae2f_mAnnSp ae2f_AnnSlpEl; /// @brief /// # Single Layered Perceptron @@ -68,7 +68,7 @@ typedef struct ae2f_AnnSlp { /// @brief /// True when its shape is same as expected. \n - /// Expected structure is generated by @ref ae2f_AnnSpInit. + /// Expected structure is generated by @ref ae2f_mAnnSpInit. int expected; /// @brief @@ -104,7 +104,7 @@ typedef struct ae2f_AnnSlp { /// @brief /// Input padding for perceptron. /// -/// Valid when @ref ae2f_AnnSp::expected is 1. \n +/// Valid when @ref ae2f_mAnnSp::expected is 1. \n /// When not, it is null. #define ae2f_AnnSlpPerVPad(slp, ...) \ (ae2f_CmpGetMem(slp, expected, 0) ? \ @@ -135,28 +135,28 @@ ae2f_reinterpret_cast(__VA_ARGS__ type, ae2f_CmpGetMem(slp, expected, 0) ? (ae2f /// @memberof ae2f_AnnSlp /// @brief /// Caller of @ref ae2f_AnnSlpPredict_t. -#define ae2f_AnnSlpPredict ae2f_AnnSpPredict +#define ae2f_AnnSlpPredict ae2f_mAnnSpPredict /// @memberof ae2f_AnnSlp /// @brief /// Caller of @ref ae2f_AnnSlpTrain_t. -#define ae2f_AnnSlpTrain ae2f_AnnSpTrain +#define ae2f_AnnSlpTrain ae2f_mAnnSpTrain /// @memberof ae2f_AnnSlp /// @brief /// Caller of @ref ae2f_AnnSlpTrain_t \n /// Train with Pre-calculated delta. -#define ae2f_AnnSlpTrainA ae2f_AnnSpTrainA +#define ae2f_AnnSlpTrainA ae2f_mAnnSpTrainA /// @memberof ae2f_AnnSlp /// @brief /// Caller of @ref ae2f_AnnSlpTrain_t \n /// Train for desired output: goal. -#define ae2f_AnnSlpTrainB ae2f_AnnSpTrainB +#define ae2f_AnnSlpTrainB ae2f_mAnnSpTrainB /// @memberof ae2f_AnnSlp /// @brief Cleaning function -#define ae2f_AnnSlpClean ae2f_AnnSpClean +#define ae2f_AnnSlpClean ae2f_mAnnSpClean /// @memberof ae2f_AnnSlp /// @brief @@ -251,10 +251,10 @@ ae2f_AnnSlpInit(_this, 0, ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_o /// @memberof ae2f_AnnSlp /// @brief -/// Makes a typical( @ref ae2f_AnnSp::expected ) perceptron. +/// Makes a typical( @ref ae2f_mAnnSp::expected ) perceptron. /// See @ref ae2f_AnnSlpInit. /// -/// It is heap-allocated. pass the output @ref ae2f_AnnSpDel after use. +/// It is heap-allocated. pass the output @ref ae2f_mAnnSpDel after use. ae2f_extern ae2f_SHAREDCALL ae2f_AnnSlp* ae2f_AnnSlpMk( const size_t* incs_optA, @@ -270,19 +270,19 @@ ae2f_AnnSlp* ae2f_AnnSlpMk( /// @memberof ae2f_AnnSlp /// @brief -/// Makes a typical( @ref ae2f_AnnSp::expected ) perceptron. +/// Makes a typical( @ref ae2f_mAnnSp::expected ) perceptron. /// See @ref ae2f_AnnSlpInit. /// -/// It is heap-allocated. pass the output @ref ae2f_AnnSpDel after use. +/// It is heap-allocated. pass the output @ref ae2f_mAnnSpDel after use. #define ae2f_AnnSlpMkA(incs, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) \ ae2f_AnnSlpMk(incs, 0, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) /// @memberof ae2f_AnnSlp /// @brief -/// Makes a typical( @ref ae2f_AnnSp::expected ) perceptron. +/// Makes a typical( @ref ae2f_mAnnSp::expected ) perceptron. /// See @ref ae2f_AnnSlpInit. /// -/// It is heap-allocated. pass the output @ref ae2f_AnnSpDel after use. +/// It is heap-allocated. pass the output @ref ae2f_mAnnSpDel after use. #define ae2f_AnnSlpMkB(ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) \ ae2f_AnnSlpMk(0, ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) diff --git a/include/ae2f/Ann/Slp.h.cxx/Slp.hh b/include/ae2f/Ann/Slp.h.cxx/Slp.hh index 661ca4a..6f3a10e 100644 --- a/include/ae2f/Ann/Slp.h.cxx/Slp.hh +++ b/include/ae2f/Ann/Slp.h.cxx/Slp.hh @@ -18,15 +18,15 @@ inline ae2f_err_t ae2f_TMP Predict( ae2f_float_t* out_opt ) const noexcept; -inline ae2f_err_t ae2f_TMP Train( +inline ae2f_err_t ae2f_TMP TrainA( const ae2f_float_t* in, const ae2f_float_t* delta_optA, ae2f_float_t learningrate ) noexcept; -inline ae2f_err_t ae2f_TMP Train( +inline ae2f_err_t ae2f_TMP TrainB( const ae2f_float_t* in, - ae2f_float_t* goal_optB, + const ae2f_float_t* goal_optB, ae2f_float_t learningrate ) noexcept; diff --git a/include/ae2f/Ann/Slp.h.cxx/imp.hh b/include/ae2f/Ann/Slp.h.cxx/imp.hh index e05878f..cfee089 100644 --- a/include/ae2f/Ann/Slp.h.cxx/imp.hh +++ b/include/ae2f/Ann/Slp.h.cxx/imp.hh @@ -21,7 +21,7 @@ inline ae2f_err_t ae2f_TMP Predict( return ae2f_AnnSlpPredict(this, in, out_opt); } -inline ae2f_err_t ae2f_TMP Train( +inline ae2f_err_t ae2f_TMP TrainA( const ae2f_float_t* in, const ae2f_float_t* delta_optA, ae2f_float_t learningrate @@ -29,9 +29,9 @@ inline ae2f_err_t ae2f_TMP Train( return ae2f_AnnSlpTrainA(this, in, delta_optA, learningrate); } -inline ae2f_err_t ae2f_TMP Train( +inline ae2f_err_t ae2f_TMP TrainB( const ae2f_float_t* in, - ae2f_float_t* goal_optB, + const ae2f_float_t* goal_optB, ae2f_float_t learningrate ) noexcept { return ae2f_AnnSlpTrainB(this, in, goal_optB, learningrate); diff --git a/include/ae2f/Ann/Sp.h b/include/ae2f/Ann/Sp.h index 703fff0..cdf814d 100644 --- a/include/ae2f/Ann/Sp.h +++ b/include/ae2f/Ann/Sp.h @@ -22,19 +22,19 @@ #include "Sp.h.c/cleandef.h" -typedef struct ae2f_AnnSp ae2f_AnnSp; +typedef struct ae2f_mAnnSp ae2f_mAnnSp; /// @brief /// Predict one output from multiple inputs. /// @param[in] in /// Input vector. \n -/// Its count is @ref ae2f_AnnSp::inc. +/// Its count is @ref ae2f_mAnnSp::inc. /// @param[out] outret_opt /// That one predicted. \n /// its count is 1. /// @return State -typedef ae2f_err_t ae2f_AnnSpPredict_t ( - const ae2f_AnnSp* _this, +typedef ae2f_err_t ae2f_mAnnSpPredict_t ( + const ae2f_mAnnSp* _this, const ae2f_float_t* in, ae2f_float_t* outret_opt ) noexcept; @@ -43,7 +43,7 @@ typedef ae2f_err_t ae2f_AnnSpPredict_t ( /// Train for desired output: @ref goal. /// @param[in] in /// Input vector. \n -/// Its count is @ref ae2f_AnnSp::inc. +/// Its count is @ref ae2f_mAnnSp::inc. /// @param[in] delta_optA /// Delta vector when you pre-calculated it. /// Its count is one. @@ -51,8 +51,8 @@ typedef ae2f_err_t ae2f_AnnSpPredict_t ( /// Desired output. /// @param learningrate /// Learning rate -typedef ae2f_err_t ae2f_AnnSpTrain_t ( - ae2f_AnnSp* _this, +typedef ae2f_err_t ae2f_mAnnSpTrain_t ( + ae2f_mAnnSp* _this, const ae2f_float_t* in, const ae2f_float_t* delta_optA, ae2f_float_t goal_optB, @@ -61,18 +61,16 @@ typedef ae2f_err_t ae2f_AnnSpTrain_t ( /// @brief /// Clean the class. -typedef ae2f_err_t ae2f_AnnSpClean_t (ae2f_AnnSp*) noexcept; - -#undef ae2f_AnnSp +typedef ae2f_err_t ae2f_mAnnSpClean_t (ae2f_mAnnSp*) noexcept; /// @brief /// # Single Perceptron /// /// Multiple input, one output. -struct ae2f_AnnSp { +struct ae2f_mAnnSp { /// @brief /// True when its shape is same as expected. \n - /// Expected structure is generated by @ref ae2f_AnnSpInit. + /// Expected structure is generated by @ref ae2f_mAnnSpInit. int expected; /// @brief count of inputs to predict. @@ -84,62 +82,62 @@ struct ae2f_AnnSp { ae2f_AnnAct_t* Act; /// @brief Cleaning function - ae2f_AnnSpClean_t* vClean; + ae2f_mAnnSpClean_t* vClean; /// @brief Predict function - ae2f_AnnSpPredict_t* vPredict; + ae2f_mAnnSpPredict_t* vPredict; /// @brief Training function - ae2f_AnnSpTrain_t* vTrain; + ae2f_mAnnSpTrain_t* vTrain; #if ae2f_WhenCXX(1) + 0 - #include "Sp.h.cxx/Sp.hh" + #include "Sp.h.cxx/mSp.hh" #endif }; -/// @memberof ae2f_AnnSp +/// @memberof ae2f_mAnnSp /// @brief /// Bias Address. \n /// Its count is @ref 1. /// -/// Valid when @ref ae2f_AnnSp::expected is 1. \n +/// Valid when @ref ae2f_mAnnSp::expected is 1. \n /// When not, it is null. -#define ae2f_AnnSpB(_this, ...) (ae2f_CmpGetMem(_this, expected, 0) ? ae2f_reinterpret_cast(__VA_ARGS__ ae2f_float_t*, ae2f_static_cast(__VA_ARGS__ ae2f_AnnSp*, _this) + 1) : 0) +#define ae2f_mAnnSpB(_this, ...) (ae2f_CmpGetMem(_this, expected, 0) ? ae2f_reinterpret_cast(__VA_ARGS__ ae2f_float_t*, ae2f_static_cast(__VA_ARGS__ ae2f_mAnnSp*, _this) + 1) : 0) -/// @memberof ae2f_AnnSp +/// @memberof ae2f_mAnnSp /// @brief /// Actual Weights Vector. \n -/// Its count is @ref ae2f_AnnSp::inc. +/// Its count is @ref ae2f_mAnnSp::inc. /// -/// Valid when @ref ae2f_AnnSp::expected is 1. \n +/// Valid when @ref ae2f_mAnnSp::expected is 1. \n /// When not, it is null. -#define ae2f_AnnSpW(_this, ...) (ae2f_CmpGetMem(_this, expected, 0) ? (ae2f_AnnSpB(_this, __VA_ARGS__) + 1) : 0) +#define ae2f_mAnnSpW(_this, ...) (ae2f_CmpGetMem(_this, expected, 0) ? (ae2f_mAnnSpB(_this, __VA_ARGS__) + 1) : 0) -/// @memberof ae2f_AnnSp +/// @memberof ae2f_mAnnSp /// @brief /// Additional allocated byte vector. /// -/// Valid when @ref ae2f_AnnSp::expected is 1. \n +/// Valid when @ref ae2f_mAnnSp::expected is 1. \n /// When not, it is null. /// @param prm_type The return value. -#define ae2f_AnnSpX(_this, prm_type, ...) ((_this) ? (_this)->expected ? ae2f_reinterpret_cast(__VA_ARGS__ prm_type, ae2f_AnnSpW(_this, __VA_ARGS__) + (_this)->inc) : ae2f_reinterpret_cast(__VA_ARGS__ prm_type, ae2f_static_cast(__VA_ARGS__ ae2f_AnnSp*, _this) + 1) : 0) +#define ae2f_mAnnSpX(_this, prm_type, ...) ((_this) ? (_this)->expected ? ae2f_reinterpret_cast(__VA_ARGS__ prm_type, ae2f_mAnnSpW(_this, __VA_ARGS__) + (_this)->inc) : ae2f_reinterpret_cast(__VA_ARGS__ prm_type, ae2f_static_cast(__VA_ARGS__ ae2f_mAnnSp*, _this) + 1) : 0) -/// @memberof ae2f_AnnSp +/// @memberof ae2f_mAnnSp /// @brief /// Calculates the least byte size for your design of structure. \n -/// See @ref ae2f_AnnSpInit +/// See @ref ae2f_mAnnSpInit /// /// @param off /// Additional allocating size for some reason. \n -/// Accessible with @ref ae2f_AnnSpX. +/// Accessible with @ref ae2f_mAnnSpX. /// /// @param inc /// Count of weight vector. -#define ae2f_AnnSpInitSz(off, inc) ((off + 1) + sizeof(ae2f_AnnSp) + (inc) * sizeof(ae2f_float_t)) +#define ae2f_mAnnSpInitSz(off, inc) ((off + 1) + sizeof(ae2f_mAnnSp) + (inc) * sizeof(ae2f_float_t)) -/// @memberof ae2f_AnnSp +/// @memberof ae2f_mAnnSp /// @brief -/// Initialises a typical( @ref ae2f_AnnSp::expected ) perceptron. +/// Initialises a typical( @ref ae2f_mAnnSp::expected ) perceptron. /// @param[out] _this_opt /// @param inum /// Input count, (aka weights count, inc) @@ -155,10 +153,10 @@ struct ae2f_AnnSp { /// @return /// Size of param [_this] needed. \n /// @ref -/// ae2f_AnnSpInitSz(offset_opt, inum) +/// ae2f_mAnnSpInitSz(offset_opt, inum) ae2f_extern ae2f_SHAREDCALL -size_t ae2f_AnnSpInit( - ae2f_AnnSp* _this_opt, +size_t ae2f_mAnnSpInit( + ae2f_mAnnSp* _this_opt, size_t inum, const ae2f_float_t* W_opt, ae2f_fpAnnAct_t Act, @@ -167,14 +165,22 @@ size_t ae2f_AnnSpInit( size_t offset_opt ) noexcept; -/// @memberof ae2f_AnnSp +typedef union ae2f_AnnSp { + ae2f_mAnnSp Sp; + + #if ae2f_WhenCXX(!)0 + #include "Sp.h.cxx/Sp.hh" + #endif +} ae2f_AnnSp; + +/// @memberof ae2f_mAnnSp /// @brief -/// Makes a typical( @ref ae2f_AnnSp::expected ) perceptron. -/// See @ref ae2f_AnnSpInit. +/// Makes a typical( @ref ae2f_mAnnSp::expected ) perceptron. +/// See @ref ae2f_mAnnSpInit. /// -/// It is heap-allocated. pass the output @ref ae2f_AnnSpDel after use. +/// It is heap-allocated. pass the output @ref ae2f_mAnnSpDel after use. ae2f_extern ae2f_SHAREDCALL -ae2f_AnnSp* ae2f_AnnSpMk( +ae2f_AnnSp* ae2f_mAnnSpMk( size_t inum, const ae2f_float_t* W_opt, ae2f_fpAnnAct_t Act, @@ -183,13 +189,13 @@ ae2f_AnnSp* ae2f_AnnSpMk( size_t offset_opt ) noexcept; -/// @memberof ae2f_AnnSp +/// @memberof ae2f_mAnnSp /// @brief /// Clean the class. -#define ae2f_AnnSpClean(_this) \ +#define ae2f_mAnnSpClean(_this) \ ((_this) ? (_this)->vClean ? (_this)->vClean(_this) : ae2f_errGlob_OK : ae2f_errGlob_PTR_IS_NULL) -/// @memberof ae2f_AnnSp +/// @memberof ae2f_mAnnSp /// @brief /// Clean the class and it will try to free the pointer. /// @param[out] _this @@ -198,34 +204,34 @@ ae2f_AnnSp* ae2f_AnnSpMk( /// @memberof ae2f_AnnSlp /// @param[out] _this /// This memory will be freed. -ae2f_AnnDelDef(ae2f_AnnSp) noexcept { +ae2f_AnnDelDef(ae2f_mAnnSp) noexcept { #include "Sp.h.c/clean.script.h" } -/// @memberof ae2f_AnnSp +/// @memberof ae2f_mAnnSp /// @brief -/// Caller of @ref ae2f_AnnSpPredict_t -#define ae2f_AnnSpPredict(_this, in, out_opt) \ +/// Caller of @ref ae2f_mAnnSpPredict_t +#define ae2f_mAnnSpPredict(_this, in, out_opt) \ (ae2f_CmpGetMem(_this, vPredict, 0) ? (_this)->vPredict(_this, in, out_opt) : (ae2f_errGlob_IMP_NOT_FOUND | ae2f_errGlob_PTR_IS_NULL)) -/// @memberof ae2f_AnnSp +/// @memberof ae2f_mAnnSp /// @brief -/// Caller of @ref ae2f_AnnSpTrain_t -#define ae2f_AnnSpTrain(_this, in, delta_optA, goal_optB, learningrate) \ +/// Caller of @ref ae2f_mAnnSpTrain_t +#define ae2f_mAnnSpTrain(_this, in, delta_optA, goal_optB, learningrate) \ (ae2f_CmpGetMem(_this, vTrain, 0) ? (_this)->vTrain(_this, in, delta_optA, goal_optB, learningrate) : (ae2f_errGlob_IMP_NOT_FOUND | ae2f_errGlob_PTR_IS_NULL)) -/// @memberof ae2f_AnnSp +/// @memberof ae2f_mAnnSp /// @brief -/// Caller of @ref ae2f_AnnSpTrain_t \n +/// Caller of @ref ae2f_mAnnSpTrain_t \n /// Train with Pre-calculated delta. -#define ae2f_AnnSpTrainA(_this, in, predelta, learningrate) \ -ae2f_AnnSpTrain(_this, in, predelta, 0, learningrate) +#define ae2f_mAnnSpTrainA(_this, in, predelta, learningrate) \ +ae2f_mAnnSpTrain(_this, in, predelta, 0, learningrate) -/// @memberof ae2f_AnnSp +/// @memberof ae2f_mAnnSp /// @brief /// Train for desired output: goal. -#define ae2f_AnnSpTrainB(_this, in, goal, learningrate) \ -ae2f_AnnSpTrain(_this, in, 0, goal, learningrate) +#define ae2f_mAnnSpTrainB(_this, in, goal, learningrate) \ +ae2f_mAnnSpTrain(_this, in, 0, goal, learningrate) #include diff --git a/include/ae2f/Ann/Sp.h.cxx/Sp.hh b/include/ae2f/Ann/Sp.h.cxx/Sp.hh index 6618a06..2fa56d4 100644 --- a/include/ae2f/Ann/Sp.h.cxx/Sp.hh +++ b/include/ae2f/Ann/Sp.h.cxx/Sp.hh @@ -1,49 +1,11 @@ -/** - * @file Sp.hh - * @author ae2f - * @brief - * @version 0.2 - * @date 2025-02-09 - * - * @copyright Copyright (c) 2025 - * - */ - -#include "../Sp.h" - #define ae2f_TMP - -inline ae2f_err_t ae2f_TMP Predict( - const ae2f_float_t* in, - ae2f_float_t* out_opt -) const noexcept; - -inline ae2f_err_t ae2f_TMP Train( - const ae2f_float_t* in, - const ae2f_float_t* delta_optA, - ae2f_float_t learningrate -) noexcept; - -inline ae2f_err_t ae2f_TMP Train( - const ae2f_float_t* in, - ae2f_float_t goal_optB, - ae2f_float_t learningrate -) noexcept; - -template -constexprfun const T* ae2f_TMP X() const; - -template -constexprfun T* ae2f_TMP X(); -static constexprfun size_t ae2f_TMP sz(size_t off, size_t inc); inline ae2f_TMP ~ae2f_AnnSp(); -constexprfun const ae2f_float_t* ae2f_TMP B() const; -constexprfun ae2f_float_t* ae2f_TMP B(); +private: -constexprfun const ae2f_float_t* ae2f_TMP W() const; -constexprfun ae2f_float_t* ae2f_TMP W(); +uint8_t _; +constexprfun ae2f_AnnSp() : _(0) {} #undef ae2f_TMP \ No newline at end of file diff --git a/include/ae2f/Ann/Sp.h.cxx/imp.hh b/include/ae2f/Ann/Sp.h.cxx/imp.hh index e4fd60a..0c7ec4d 100644 --- a/include/ae2f/Ann/Sp.h.cxx/imp.hh +++ b/include/ae2f/Ann/Sp.h.cxx/imp.hh @@ -11,61 +11,69 @@ #include "../Sp.h" -#define ae2f_TMP ae2f_AnnSp:: +#define ae2f_TMP ae2f_mAnnSp:: inline ae2f_err_t ae2f_TMP Predict( const ae2f_float_t* in, ae2f_float_t* out_opt ) const noexcept { - return ae2f_AnnSpPredict(this, in, out_opt); + return ae2f_mAnnSpPredict(this, in, out_opt); } -inline ae2f_err_t ae2f_TMP Train( +inline ae2f_err_t ae2f_TMP TrainA( const ae2f_float_t* in, const ae2f_float_t* delta_optA, ae2f_float_t learningrate ) noexcept { - return ae2f_AnnSpTrainA(this, in, delta_optA, learningrate); + return ae2f_mAnnSpTrainA(this, in, delta_optA, learningrate); } -inline ae2f_err_t ae2f_TMP Train( +inline ae2f_err_t ae2f_TMP TrainB( const ae2f_float_t* in, ae2f_float_t goal_optB, ae2f_float_t learningrate ) noexcept { - return ae2f_AnnSpTrainB(this, in, goal_optB, learningrate); + return ae2f_mAnnSpTrainB(this, in, goal_optB, learningrate); } constexprfun const ae2f_float_t* ae2f_TMP B() const { - return ae2f_AnnSpB(this, const); + return ae2f_mAnnSpB(this, const); } constexprfun ae2f_float_t* ae2f_TMP B() { - return ae2f_AnnSpB(this); + return ae2f_mAnnSpB(this); } constexprfun const ae2f_float_t* ae2f_TMP W() const { - return ae2f_AnnSpW(this, const); + return ae2f_mAnnSpW(this, const); } constexprfun ae2f_float_t* ae2f_TMP W() { - return ae2f_AnnSpW(this, ); + return ae2f_mAnnSpW(this, ); } template constexprfun const T* ae2f_TMP X() const { - return ae2f_AnnSpX(this, T*, const); + return ae2f_mAnnSpX(this, T*, const); } template constexprfun T* ae2f_TMP X() { - return ae2f_AnnSpX(this, T*, ); + return ae2f_mAnnSpX(this, T*, ); } -inline ae2f_TMP ~ae2f_AnnSp() { - ae2f_AnnSpClean(this); +inline ae2f_TMP ~ae2f_mAnnSp() { + ae2f_mAnnSpClean(this); } constexprfun size_t ae2f_TMP sz(size_t off, size_t inc) { - return ae2f_AnnSpInitSz(off, inc); + return ae2f_mAnnSpInitSz(off, inc); +} + +#undef ae2f_TMP + +#define ae2f_TMP ae2f_AnnSp:: + +inline ae2f_TMP ~ae2f_AnnSp() { + ae2f_mAnnSpClean(&this->Sp); } #undef ae2f_TMP \ No newline at end of file diff --git a/include/ae2f/Ann/Sp.h.cxx/mSp.hh b/include/ae2f/Ann/Sp.h.cxx/mSp.hh new file mode 100644 index 0000000..9b74004 --- /dev/null +++ b/include/ae2f/Ann/Sp.h.cxx/mSp.hh @@ -0,0 +1,49 @@ +/** + * @file Sp.hh + * @author ae2f + * @brief + * @version 0.2 + * @date 2025-02-09 + * + * @copyright Copyright (c) 2025 + * + */ + +#include "../Sp.h" + +#define ae2f_TMP + +inline ae2f_err_t ae2f_TMP Predict( + const ae2f_float_t* in, + ae2f_float_t* out_opt +) const noexcept; + +inline ae2f_err_t ae2f_TMP TrainA( + const ae2f_float_t* in, + const ae2f_float_t* delta_optA, + ae2f_float_t learningrate +) noexcept; + +inline ae2f_err_t ae2f_TMP TrainB( + const ae2f_float_t* in, + ae2f_float_t goal_optB, + ae2f_float_t learningrate +) noexcept; + +template +constexprfun const T* ae2f_TMP X() const; + +template +constexprfun T* ae2f_TMP X(); + +static constexprfun size_t ae2f_TMP sz(size_t off, size_t inc); +inline ae2f_TMP ~ae2f_mAnnSp(); + +constexprfun const ae2f_float_t* ae2f_TMP B() const; +constexprfun ae2f_float_t* ae2f_TMP B(); + +constexprfun const ae2f_float_t* ae2f_TMP W() const; +constexprfun ae2f_float_t* ae2f_TMP W(); + + +#undef ae2f_TMP \ No newline at end of file diff --git a/include/ae2fCL/Ann/Sp.h b/include/ae2fCL/Ann/Sp.h index f889a81..d3886a9 100644 --- a/include/ae2fCL/Ann/Sp.h +++ b/include/ae2fCL/Ann/Sp.h @@ -7,7 +7,7 @@ * * @copyright Copyright (c) 2025 * - * I am adding @ref ae2f_AnnSp ability to + * I am adding @ref ae2f_mAnnSp ability to * make calculation parallelly with OpenCL. * * While keeping the basic structure of base class. @@ -22,29 +22,29 @@ #include #if ae2f_WhenCXX(!) 0 -struct ae2fCL_AnnSp : ae2f_AnnSp { +struct ae2fCL_AnnSp : ae2f_mAnnSp { #include "./Sp.h.cxx/Sp.hh" }; #else /// @class ae2fCL_AnnSp -/// @extends ae2f_AnnSp +/// @extends ae2f_mAnnSp /// @brief /// Hello World -typedef ae2f_AnnSp ae2fCL_AnnSp; +typedef ae2f_mAnnSp ae2fCL_AnnSp; #endif -#define ae2fCL_AnnSpPredict ae2f_AnnSpPredict -#define ae2fCL_AnnSpTrain ae2f_AnnSpTrain -#define ae2fCL_AnnSpTrainA ae2f_AnnSpTrainA -#define ae2fCL_AnnSpTrainB ae2f_AnnSpTrainB -#define ae2fCL_AnnSpClean ae2f_AnnSpClean -#define ae2fCL_AnnSpB ae2f_AnnSpB -#define ae2fCL_AnnSpW ae2f_AnnSpW -#define ae2fCL_AnnSpDel ae2f_AnnSpDel +#define ae2fCL_AnnSpPredict ae2f_mAnnSpPredict +#define ae2fCL_AnnSpTrain ae2f_mAnnSpTrain +#define ae2fCL_AnnSpTrainA ae2f_mAnnSpTrainA +#define ae2fCL_AnnSpTrainB ae2f_mAnnSpTrainB +#define ae2fCL_AnnSpClean ae2f_mAnnSpClean +#define ae2fCL_AnnSpB ae2f_mAnnSpB +#define ae2fCL_AnnSpW ae2f_mAnnSpW +#define ae2fCL_AnnSpDel ae2f_mAnnSpDel -#define ae2fCL_AnnSpInitSz(off, inc) ae2f_AnnSpInitSz((off) + (cl_mem_SIZE << 2), inc) -#define ae2fCL_AnnSpWCl(per, ...) ae2f_AnnSpX(per, cl_mem*, __VA_ARGS__) +#define ae2fCL_AnnSpInitSz(off, inc) ae2f_mAnnSpInitSz((off) + (cl_mem_SIZE << 2), inc) +#define ae2fCL_AnnSpWCl(per, ...) ae2f_mAnnSpX(per, cl_mem*, __VA_ARGS__) #define ae2fCL_AnnSpIOCl(per, ...) (ae2fCL_AnnSpWCl(per, __VA_ARGS__) + 1) #define ae2fCL_AnnSpX(per, type, ...) ae2f_reinterpret_cast(__VA_ARGS__ type, ae2fCL_AnnSpIOCl(per, __VA_ARGS__) + 1) diff --git a/src/ann-h/Mlp/HidErr.h b/src/ann-h/Mlp/HidErr.h index 9f77b88..171d848 100644 --- a/src/ann-h/Mlp/HidErr.h +++ b/src/ann-h/Mlp/HidErr.h @@ -15,9 +15,9 @@ static ae2f_float_t MlpTrain_HidErr( for(size_t i = 0; i < layerNxt->outc; i++) { union { const size_t* pad; - const ae2f_AnnSp* slp; + const ae2f_mAnnSp* slp; } perv = { .pad = ae2f_AnnSlpPerVPad(layerNxt)[i] + 1 }; - ret += ae2f_AnnSpW(perv.slp, const)[idxThen] * deltasNxt[i]; + ret += ae2f_mAnnSpW(perv.slp, const)[idxThen] * deltasNxt[i]; } return ret; diff --git a/src/ann-h/Slp.h b/src/ann-h/Slp.h index 481fbd5..510c660 100644 --- a/src/ann-h/Slp.h +++ b/src/ann-h/Slp.h @@ -20,14 +20,14 @@ static ae2f_err_t Predict( for(size_t i = 0; i < _this->outc; i++) { union { const size_t* pad; - const ae2f_AnnSp* perc; + const ae2f_mAnnSp* perc; } layer = {ae2f_AnnSlpPerVPad(_this, const)[i]}; size_t _pad = *layer.pad; layer.pad++; ae2f_err_t er = - ae2f_AnnSpPredict( + ae2f_mAnnSpPredict( layer.perc, in + _pad, out_opt + i @@ -57,13 +57,13 @@ static ae2f_err_t Train( ae2f_float_t G = goal_optB ? goal_optB[i] : 0; union { size_t* pad; - ae2f_AnnSp* perc; + ae2f_mAnnSp* perc; } layer = {ae2f_AnnSlpPerVPad(_this)[i]}; size_t _pad = *layer.pad; layer.pad++; - _er |= ae2f_AnnSpTrain( + _er |= ae2f_mAnnSpTrain( layer.perc, in + _pad, delta_optA, G, @@ -77,13 +77,13 @@ static ae2f_err_t Train( ae2f_float_t G = goal_optB ? goal_optB[i] : 0; union { size_t* pad; - ae2f_AnnSp* perc; + ae2f_mAnnSp* perc; } layer = {ae2f_AnnSlpPerVPad(_this)[i]}; size_t _pad = *layer.pad; layer.pad++; - _er |= ae2f_AnnSpTrain( + _er |= ae2f_mAnnSpTrain( layer.perc, in + _pad, delta_optA, G, @@ -101,10 +101,10 @@ static ae2f_err_t Clean( for(size_t i = 0; i < _this->layerc; i++) { const union { - ae2f_AnnSp* v; + ae2f_mAnnSp* v; size_t* S; } per = { .v = ae2f_AnnSlpPerV(_this, i)}; - ae2f_AnnSpClean(per.v); + ae2f_mAnnSpClean(per.v); free(per.S - 1); ae2f_AnnSlpPerVPad(_this, )[i] = 0; diff --git a/src/ann-h/Sp.h b/src/ann-h/Sp.h index 081371b..bd2d0b7 100644 --- a/src/ann-h/Sp.h +++ b/src/ann-h/Sp.h @@ -18,15 +18,15 @@ #include #include -#define ae2f_AnnSpPredictI(_this, in, i, ...) (ae2f_AnnSpW(_this, __VA_ARGS__ const)[i] * (in)[i]) -#define ae2f_AnnSpTrainI(_this, in, i, ...) (ae2f_AnnSpW(_this, __VA_ARGS__)[i] += _delta * (in)[i]) +#define ae2f_mAnnSpPredictI(_this, in, i, ...) (ae2f_mAnnSpW(_this, __VA_ARGS__ const)[i] * (in)[i]) +#define ae2f_mAnnSpTrainI(_this, in, i, ...) (ae2f_mAnnSpW(_this, __VA_ARGS__)[i] += _delta * (in)[i]) -static ae2f_AnnSpPredict_t Predict; -static ae2f_AnnSpTrain_t Train; +static ae2f_mAnnSpPredict_t Predict; +static ae2f_mAnnSpTrain_t Train; static ae2f_err_t Predict( - const ae2f_AnnSp* _this, + const ae2f_mAnnSp* _this, const ae2f_float_t* in, ae2f_float_t* out_opt ) { @@ -36,10 +36,10 @@ ae2f_err_t Predict( ae2f_float_t sum = 0; for(size_t i = 0; i < _this->inc; i++) { - sum += ae2f_AnnSpPredictI(_this, in, i); + sum += ae2f_mAnnSpPredictI(_this, in, i); } - sum += *ae2f_AnnSpB(_this); + sum += *ae2f_mAnnSpB(_this); if(_this->Act) sum = (_this)->Act(sum); @@ -50,7 +50,7 @@ ae2f_err_t Predict( static ae2f_err_t Train( - ae2f_AnnSp* _this, + ae2f_mAnnSp* _this, const ae2f_float_t* in, const ae2f_float_t* delta_optA, ae2f_float_t goal_optB, @@ -67,7 +67,7 @@ ae2f_err_t Train( if(delta_optA) _delta = *delta_optA; else { - err = ae2f_AnnSpPredict(_this, in, &_delta); + err = ae2f_mAnnSpPredict(_this, in, &_delta); if(err) goto __DONE; _delta = _this->CalDelta(_delta, goal_optB); } @@ -75,10 +75,10 @@ ae2f_err_t Train( _delta *= learningrate; for(size_t i = 0; i < _this->inc; i++) { - ae2f_AnnSpW(_this, )[i] += _delta * in[i]; + ae2f_mAnnSpW(_this, )[i] += _delta * in[i]; } - *ae2f_AnnSpB(_this) += _delta; + *ae2f_mAnnSpB(_this) += _delta; __DONE: return err; diff --git a/src/ann/Slp.c b/src/ann/Slp.c index 08620ef..acfe19e 100644 --- a/src/ann/Slp.c +++ b/src/ann/Slp.c @@ -36,13 +36,13 @@ size_t ae2f_AnnSlpInit( _pad = inpads_opt ? inpads_opt[i] : 0; if(!(ae2f_AnnSlpPerVPad(_this)[i] - = calloc(ae2f_AnnSpInitSz(sizeof(size_t), _inc), 1))) + = calloc(ae2f_mAnnSpInitSz(sizeof(size_t), _inc), 1))) { er |= ae2f_errGlob_ALLOC_FAILED; continue; } - ae2f_AnnSpInit( + ae2f_mAnnSpInit( ae2f_AnnSlpPerV(_this, i), _inc, w_opt, Act, CalDelta, diff --git a/src/ann/Sp.c b/src/ann/Sp.c index 0987285..1c178a6 100644 --- a/src/ann/Sp.c +++ b/src/ann/Sp.c @@ -14,8 +14,8 @@ #include ae2f_SHAREDEXPORT -size_t ae2f_AnnSpInit( - ae2f_AnnSp* _this, +size_t ae2f_mAnnSpInit( + ae2f_mAnnSp* _this, size_t inc, const ae2f_float_t* W_opt, ae2f_fpAnnAct_t Act, @@ -42,14 +42,14 @@ size_t ae2f_AnnSpInit( _this->vClean = 0; if(W_opt) { - memcpy(ae2f_AnnSpW(_this), W_opt, sizeof(ae2f_float_t) * inc); + memcpy(ae2f_mAnnSpW(_this), W_opt, sizeof(ae2f_float_t) * inc); } else { uint64_t _ = ae2f_AnnLcgRandSeed.u64; for(size_t i = 0; i < _this->inc; i++) { _ = ae2f_AnnLcgRand(_); ae2f_float_t d = ae2f_AnnLcgRandDistribute(_); - ae2f_AnnSpW(_this)[i] = d; + ae2f_mAnnSpW(_this)[i] = d; } ae2f_AnnLcgRandSeed.u64 = _; } @@ -57,16 +57,16 @@ size_t ae2f_AnnSpInit( { uint64_t _ = ae2f_AnnLcgRandG(); ae2f_AnnLcgRandSeed.u64 = _; - *ae2f_AnnSpB(_this) = ae2f_AnnLcgRandDistribute(_); + *ae2f_mAnnSpB(_this) = ae2f_AnnLcgRandDistribute(_); } DONE: if(erret) *erret = er; - return ae2f_AnnSpInitSz(offset_opt, inc); + return ae2f_mAnnSpInitSz(offset_opt, inc); } ae2f_SHAREDEXPORT -ae2f_AnnSp* ae2f_AnnSpMk( +ae2f_AnnSp* ae2f_mAnnSpMk( size_t inc, const ae2f_float_t* W_opt, ae2f_fpAnnAct_t Act, @@ -76,8 +76,8 @@ ae2f_AnnSp* ae2f_AnnSpMk( ) { ae2f_AnnSp* _this = 0; - _this = calloc(ae2f_AnnSpInitSz(additional, inc), 1); - ae2f_AnnSpInit(_this, inc, W_opt, Act, CalDelta, erret, additional); + _this = calloc(ae2f_mAnnSpInitSz(additional, inc), 1); + ae2f_mAnnSpInit(&_this->Sp, inc, W_opt, Act, CalDelta, erret, additional); rtn: if(erret) *erret &= ~ae2f_errGlob_DONE_HOWEV; diff --git a/src/cl/Slp.c b/src/cl/Slp.c index d24b7e6..47eb12e 100644 --- a/src/cl/Slp.c +++ b/src/cl/Slp.c @@ -90,9 +90,9 @@ ae2f_err_t TrainCL( * _padv = ae2f_AnnSlpPerVPad(_this, const)[i], pad = *_padv; - const ae2f_AnnSp* + const ae2f_mAnnSp* perc = ae2f_reinterpret_cast( - const ae2f_AnnSp*, + const ae2f_mAnnSp*, _padv + 1 ); @@ -102,7 +102,7 @@ ae2f_err_t TrainCL( ,CL_TRUE ,((IC * (1 + i)) + pad)* sizeof(ae2f_float_t) ,perc->inc * sizeof(ae2f_float_t) - ,ae2f_AnnSpW(perc, const) + ,ae2f_mAnnSpW(perc, const) , 0, NULL, 0 ); if(err2) { @@ -133,9 +133,9 @@ ae2f_err_t TrainCL( _BUFFSET: for(size_t i = 0; i < OC; i++) { - ae2f_AnnSp* perc = ae2f_AnnSlpPerV(_this, i); + ae2f_mAnnSp* perc = ae2f_AnnSlpPerV(_this, i); - *ae2f_AnnSpB(perc) += PREDICTED_BUFF[i] = + *ae2f_mAnnSpB(perc) += PREDICTED_BUFF[i] = delta_optA ? PREDICTED_BUFF[i] * learningrate : perc->CalDelta(PREDICTED_BUFF[i], goal_optB[i]) * learningrate; @@ -193,7 +193,7 @@ ae2f_err_t TrainCL( * padv = ae2f_AnnSlpPerVPad(_this)[i] , pad = *padv; - ae2f_AnnSp* perc = ae2f_reinterpret_cast(ae2f_AnnSp*, padv + 1); + ae2f_mAnnSp* perc = ae2f_reinterpret_cast(ae2f_mAnnSp*, padv + 1); if((err2 = clEnqueueReadBuffer( ae2fCL_Ann.Q @@ -201,7 +201,7 @@ ae2f_err_t TrainCL( , CL_TRUE , (IC) * (1 + i) * sizeof(ae2f_float_t) , perc->inc * sizeof(ae2f_float_t) - , ae2f_AnnSpW(perc) + , ae2f_mAnnSpW(perc) , 0, NULL, NULL ))) { @@ -290,9 +290,9 @@ ae2f_err_t PredictCL( * _padv = ae2f_AnnSlpPerVPad(_, const)[i], pad = *_padv; - const ae2f_AnnSp* + const ae2f_mAnnSp* perc = ae2f_reinterpret_cast( - const ae2f_AnnSp*, + const ae2f_mAnnSp*, _padv + 1 ); @@ -302,7 +302,7 @@ ae2f_err_t PredictCL( ,CL_TRUE ,((IC * (1 + i)) + pad)* sizeof(ae2f_float_t) ,perc->inc * sizeof(ae2f_float_t) - ,ae2f_AnnSpW(perc, const) + ,ae2f_mAnnSpW(perc, const) , 0, NULL, 0 ); if(err2) { @@ -368,13 +368,13 @@ ae2f_err_t PredictCL( for(size_t i = 0; i < OC; i++) { - const ae2f_AnnSp* + const ae2f_mAnnSp* perc = ae2f_AnnSlpPerV(_,i); if(!perc) return(ae2f_errGlob_IMP_NOT_FOUND); if(!perc->Act) return(ae2f_errGlob_IMP_NOT_FOUND); - out[i] = perc->Act(out[i] + *ae2f_AnnSpB(perc)); + out[i] = perc->Act(out[i] + *ae2f_mAnnSpB(perc)); } _DONE: @@ -431,12 +431,12 @@ size_t ae2fCL_AnnSlpInit( ae2f_AnnSlpPerVPad(_this)[i] = calloc( - ae2f_AnnSpInitSz( + ae2f_mAnnSpInitSz( sizeof(size_t), _inc ), 1 ); - ae2f_AnnSpInit( + ae2f_mAnnSpInit( ae2f_AnnSlpPerV(_this, i), _inc, w_opt, Act, CalDelta, diff --git a/src/cl/Sp.c b/src/cl/Sp.c index 2e68c16..f69c60b 100644 --- a/src/cl/Sp.c +++ b/src/cl/Sp.c @@ -3,12 +3,12 @@ #include "CLCode/Size/ae2f_float_t.auto.h" #include "CLCode/Size/cl_mem.auto.h" -static ae2f_AnnSpPredict_t Predict; -static ae2f_AnnSpTrain_t Train; -static ae2f_AnnSpClean_t Clean; +static ae2f_mAnnSpPredict_t Predict; +static ae2f_mAnnSpTrain_t Train; +static ae2f_mAnnSpClean_t Clean; static ae2f_err_t Predict( - const ae2f_AnnSp *_this, + const ae2f_mAnnSp *_this, const ae2f_float_t *in, ae2f_float_t *outret_opt ) { @@ -54,7 +54,7 @@ static ae2f_err_t Predict( #include "./CLCode/uf.h" static ae2f_err_t Train( - ae2f_AnnSp *_this, + ae2f_mAnnSp *_this, const ae2f_float_t *in, const ae2f_float_t *delta_optA, ae2f_float_t goal_optB, @@ -75,7 +75,7 @@ static ae2f_err_t Train( if(er) return er; *uf.F *= learningrate; - *ae2f_AnnSpB(_this) += *uf.F; + *ae2f_mAnnSpB(_this) += *uf.F; cl_mem _W = *ae2fCL_AnnSpWCl(_this), _IO = *ae2fCL_AnnSpIOCl(_this); cl_kernel K = ae2fCL_Ann.Kerns[ae2fCL_eAnnKernsSpTrain]; @@ -106,7 +106,7 @@ static ae2f_err_t Train( ae2fCL_Ann.Q, _W, CL_TRUE, 0, _this->inc * sizeof(ae2f_float_t), - ae2f_AnnSpW(_this), + ae2f_mAnnSpW(_this), 0, 0, 0 )) != CL_SUCCESS) return ae2f_errGlob_NFOUND; @@ -128,13 +128,13 @@ size_t ae2fCL_AnnSpInit( ae2f_err_t err = 0; cl_int err2 = 0; - ae2f_AnnSpInit(perc_opt, icount, w_opt, Act, CalDelta, &err, off_opt); + ae2f_mAnnSpInit(perc_opt, icount, w_opt, Act, CalDelta, &err, off_opt); if(err) goto END; *ae2fCL_AnnSpWCl(perc_opt) = clCreateBuffer( ae2fCL_Ann.Ctx, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, - ae2f_float_t_SIZE * icount, ae2f_AnnSpW(perc_opt), + ae2f_float_t_SIZE * icount, ae2f_mAnnSpW(perc_opt), &err2 ); if(err2 != CL_SUCCESS) @@ -169,13 +169,13 @@ ae2fCL_AnnSp* ae2fCL_AnnSpMk( cl_int* erronnfound_opt, size_t off_opt ) noexcept { - ae2f_AnnSp* v; + ae2f_mAnnSp* v; v = calloc(ae2fCL_AnnSpInitSz(off_opt, icount), 1); ae2fCL_AnnSpInit(v, icount, w_opt, Act, CalDelta, errret_opt, erronnfound_opt, off_opt); return v; } -static ae2f_err_t Clean(ae2f_AnnSp* a) { +static ae2f_err_t Clean(ae2f_mAnnSp* a) { if(!a) return ae2f_errGlob_PTR_IS_NULL; cl_int er[2]; er[0] = clReleaseMemObject(*ae2fCL_AnnSpIOCl(a)); diff --git a/test/cl-src/PercMk.cc b/test/cl-src/PercMk.cc index 66aee7b..36baca4 100644 --- a/test/cl-src/PercMk.cc +++ b/test/cl-src/PercMk.cc @@ -40,8 +40,8 @@ int mainc() { printf("out: %f\n", outfloat); printf( "Bias global: %f, with bias: %f\n", - *ae2f_AnnSpB(Perc), - (*ae2f_AnnSpB(Perc)) * sizeof(Buff)/ sizeof(ae2f_float_t) + outfloat + *ae2f_mAnnSpB(Perc), + (*ae2f_mAnnSpB(Perc)) * sizeof(Buff)/ sizeof(ae2f_float_t) + outfloat ); for(size_t i = 0; i < Perc->inc; i++) @@ -103,7 +103,7 @@ int maincc() { out_checksum += got; printf("Check-got: %f\n", got); } - out_checksum += *ae2f_AnnSpB(Perc); + out_checksum += *ae2f_mAnnSpB(Perc); out_checksum = Perc->Act(out_checksum); printf("Checking two values match...: %f %f\n", out_checksum, outfloat); if((out_checksum - outfloat) * (out_checksum - outfloat) > gThreshold) { diff --git a/test/cl-src/SlpMk.cc b/test/cl-src/SlpMk.cc index 80b8390..14e5be7 100644 --- a/test/cl-src/SlpMk.cc +++ b/test/cl-src/SlpMk.cc @@ -23,7 +23,7 @@ int mainc() { 0.3, 0.2, 0.4, 0.6, 0.1 }; - ae2f_AnnSp* Perc = 0; + ae2f_mAnnSp* Perc = 0; ae2f_AnnSlp* SLP; ae2f_float_t out_checksum = 0; @@ -45,8 +45,8 @@ int mainc() { printf("out: %f\n", outfloat); printf( "Bias global: %f, with bias: %f\n", - *ae2f_AnnSpB(Perc), - (*ae2f_AnnSpB(Perc)) * sizeof(Buff)/ sizeof(ae2f_float_t) + outfloat + *ae2f_mAnnSpB(Perc), + (*ae2f_mAnnSpB(Perc)) * sizeof(Buff)/ sizeof(ae2f_float_t) + outfloat ); for(size_t i = 0; i < Perc->inc; i++) @@ -80,7 +80,7 @@ int maincc() { 0.3, 0.2, 0.4, 0.6, 0.1 }; - ae2f_AnnSp* Perc = 0; + ae2f_mAnnSp* Perc = 0; ae2f_AnnSlp* SLP; ae2f_float_t out_checksum = 0; @@ -102,8 +102,8 @@ int maincc() { printf("out: %f\n", outfloat); printf( "Bias global: %f, with bias: %f\n", - *ae2f_AnnSpB(Perc), - (*ae2f_AnnSpB(Perc)) * sizeof(Buff)/ sizeof(ae2f_float_t) + outfloat + *ae2f_mAnnSpB(Perc), + (*ae2f_mAnnSpB(Perc)) * sizeof(Buff)/ sizeof(ae2f_float_t) + outfloat ); for(size_t i = 0; i < Perc->inc; i++) diff --git a/test/cl-src/SlpTrainAND.c b/test/cl-src/SlpTrainAND.c index 21cdfae..0913742 100644 --- a/test/cl-src/SlpTrainAND.c +++ b/test/cl-src/SlpTrainAND.c @@ -148,7 +148,7 @@ int main() { err = err2; goto __failure; } printf("Checking the value: %f\n", outbuff[0]); if(outbuff[0] > 0.5) { - err2 = ae2f_AnnSpPredict(ae2f_AnnSlpPerV(Slp, 0), ins + 6, outbuff); + err2 = ae2f_mAnnSpPredict(ae2f_AnnSlpPerV(Slp, 0), ins + 6, outbuff); printf("Checking the value (SP): %f\n", outbuff[0]); printf("AND 0, 0 no good\n"); diff --git a/test/cl-src/SpTrainAND.c b/test/cl-src/SpTrainAND.c index f7b3d8b..ff578f3 100644 --- a/test/cl-src/SpTrainAND.c +++ b/test/cl-src/SpTrainAND.c @@ -48,18 +48,18 @@ int main() { ae2fCL_AnnMkEasy(0); ae2f_err_t err2, err; - ae2f_AnnSp* Slp = ae2fCL_AnnSpMk( + ae2f_mAnnSp* Slp = ae2fCL_AnnSpMk( 2, 0, Forward, Backward, &err2, 0, 0 ); if(err2) { err = err2; goto __failure; } - #define ae2fCL_AnnSlpTrain(obj, ins, _, in_idx, __, goal, learnrate, ...) ae2f_AnnSpTrainB(obj, ins + in_idx, *(goal), learnrate) + #define ae2fCL_AnnSlpTrain(obj, ins, _, in_idx, __, goal, learnrate, ...) ae2f_mAnnSpTrainB(obj, ins + in_idx, *(goal), learnrate) if(err) goto __failure; for(size_t _ = 0; _ < gEpochs; _++) { - err2 = ae2f_AnnSpTrainB( + err2 = ae2f_mAnnSpTrainB( Slp, ins, *goals + 0, gLearningRate ); @@ -103,7 +103,7 @@ int main() { ae2f_float_t outbuff[2] = { 5 }; #define ae2fCL_AnnSlpPredict(obj, inb, _, in_idx, __, out, ...) \ - ae2f_AnnSpPredict(obj, inb + in_idx, (out)) + ae2f_mAnnSpPredict(obj, inb + in_idx, (out)) err2 = ae2fCL_AnnSlpPredict( Slp, ins, 0, @@ -154,7 +154,7 @@ int main() { } __failure: - ae2f_AnnSpDel(Slp); + ae2f_mAnnSpDel(Slp); ae2fCL_AnnDel(); return err | err2; } \ No newline at end of file diff --git a/test/src/MlpTrainXOR.cc b/test/src/MlpTrainXOR.cc index 3cc2f64..2598cb5 100644 --- a/test/src/MlpTrainXOR.cc +++ b/test/src/MlpTrainXOR.cc @@ -5,7 +5,7 @@ #include #define gLearningRate 0.1 -#define gEpochs 5000 +#define gEpochs 25000 static ae2f_float_t Step(ae2f_float_t x) { @@ -33,7 +33,7 @@ Backward(ae2f_float_t output, ae2f_float_t target) { return (target - output) * ForwardPrime(output); } -int main() { +int mainc() { ae2f_err_t err_ae2f = 0; ae2f_err_t err2 = 0; ae2f_float_t diff_got[2] = {0, }; @@ -146,4 +146,119 @@ int main() { ae2f_AnnSlpDel(&Mlp->Slp); printf("Something is over, code: %d\n", err_ae2f | err2); return err_ae2f | err2; +} + +int maincc() { + ae2f_err_t err_ae2f = 0; + ae2f_err_t err2 = 0; + ae2f_float_t diff_got[2] = {0, }; + + ae2f_AnnMlp* Mlp = 0; + size_t sizes[] = {2, 3, 1}; + ae2f_float_t outbuff[2] = { 0, 0 }; + + // [1, 1], [1, 0], [0, 1], [0, 0] + ae2f_float_t ins[] = { + 1, 1, 1, 0, 0, 1, 0, 0 + }; + + // 1, 1, 0, 0 + const ae2f_float_t goals[] = { + 1, 1, 0, 0 + }; + + Mlp = ae2f_AnnMlpMk( + sizeof(sizes) / sizeof(size_t), 0, sizes, + 0, 0, Forward, Backward, 0, + &err_ae2f + ); + + #if 1 + if(err_ae2f) { + goto __failure; + } + for(size_t _ = 0; _ < gEpochs; _++) { + err2 = Mlp->Slp.TrainB( + ins, goals + 2, gLearningRate + ); + if(err2) { + err_ae2f = err2; goto __failure; + } + + err2 = Mlp->Slp.TrainB( + ins + 2, goals, gLearningRate + ); + if(err2) { + goto __failure; + } + + err2 = Mlp->Slp.TrainB( + ins + 4, goals, gLearningRate + ); + if(err2) { + goto __failure; + } + + err2 = Mlp->Slp.TrainB( + ins + 6, goals + 2, gLearningRate + ); + if(err2) { + goto __failure; + } + } + #endif + + #if 1 + puts("Predict time"); + + err2 = ae2f_AnnSlpPredict( + &Mlp->Slp, ins, outbuff + ); if(err2) { + goto __failure; + } printf("Checking the value: %f\n", outbuff[0]); + if(outbuff[0] > 0.5) { + printf("AND 1, 1 no good\n"); + err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; + } + + err2 = ae2f_AnnSlpPredict( + &Mlp->Slp, ins + 6, outbuff + ); if(err2) { + err2 = err2; goto __failure; + } printf("Checking the value: %f\n", outbuff[0]); + if(outbuff[0] > 0.5) { + printf("AND 0, 0 no good\n"); + err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; + } + + err2 = ae2f_AnnSlpPredict( + &Mlp->Slp, ins + 4, outbuff + ); if(err2) { + err_ae2f = err2; goto __failure; + } printf("Checking the value: %f\n", outbuff[0]); + if(outbuff[0] < 0.5) { + printf("AND 0, 1 no good\n"); + err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; + } + + err2 = ae2f_AnnSlpPredict( + &Mlp->Slp, ins + 2, outbuff + ); if(err2) { + err_ae2f = err2; goto __failure; + } printf("Checking the value: %f\n", outbuff[0]); + if(outbuff[0] < 0.5) { + printf("AND 1, 0 no good\n"); + err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; + } + + #endif + + __failure: + delete Mlp; + printf("Something is over, code: %d\n", err_ae2f | err2); + return err_ae2f | err2; +} + +int main() { + return mainc() + maincc(); } \ No newline at end of file diff --git a/test/src/PercMk.cc b/test/src/PercMk.cc index 3bc1f8c..e985ecd 100644 --- a/test/src/PercMk.cc +++ b/test/src/PercMk.cc @@ -21,16 +21,16 @@ ae2f_extern int mainc() { 0.3, 0.2, 0.4, 0.6, 0.1 }; - ae2f_AnnSp* Perc; + ae2f_mAnnSp* Perc; ae2f_float_t out_checksum = 0; - Perc = ae2f_AnnSpMk( + Perc = (ae2f_mAnnSp*)ae2f_mAnnSpMk( sizeof(Buff)/sizeof(ae2f_float_t), Buff, Sigmoid, Sub, &err, 0 ); CHECK_ERR(err, 0, __failure); - err = ae2f_AnnSpPredict( + err = ae2f_mAnnSpPredict( Perc, Buff, &outfloat ); @@ -38,8 +38,8 @@ ae2f_extern int mainc() { printf("out: %f\n", outfloat); printf( "Bias global: %f, with bias: %f\n", - *ae2f_AnnSpB(Perc), - (*ae2f_AnnSpB(Perc)) * sizeof(Buff)/ sizeof(ae2f_float_t) + outfloat + *ae2f_mAnnSpB(Perc), + (*ae2f_mAnnSpB(Perc)) * sizeof(Buff)/ sizeof(ae2f_float_t) + outfloat ); for(size_t i = 0; i < Perc->inc; i++) @@ -48,7 +48,7 @@ ae2f_extern int mainc() { out_checksum += got; printf("Check-got: %f\n", got); } - out_checksum += *ae2f_AnnSpB(Perc); + out_checksum += *ae2f_mAnnSpB(Perc); out_checksum = Perc->Act(out_checksum); printf("Checking two values match...: %f %f\n", out_checksum, outfloat); if((out_checksum - outfloat) * (out_checksum - outfloat) > gThreshold) { @@ -57,7 +57,7 @@ ae2f_extern int mainc() { } __failure: - ae2f_AnnSpDel(Perc); + ae2f_mAnnSpDel(Perc); return err; } @@ -71,13 +71,13 @@ int maincc() { ae2f_AnnSp* Perc; ae2f_float_t out_checksum = 0; - Perc = ae2f_AnnSpMk( + Perc = ae2f_mAnnSpMk( sizeof(Buff)/sizeof(ae2f_float_t), Buff, Sigmoid, Sub, &err, 0 ); CHECK_ERR(err, 0, __failure); - err = Perc->Predict( + err = Perc->Sp.Predict( Buff, &outfloat ); @@ -85,18 +85,18 @@ int maincc() { printf("out: %f\n", outfloat); printf( "Bias global: %f, with bias: %f\n", - *Perc->B(), - (*Perc->B()) * sizeof(Buff)/ sizeof(ae2f_float_t) + outfloat + *Perc->Sp.B(), + (*Perc->Sp.B()) * sizeof(Buff)/ sizeof(ae2f_float_t) + outfloat ); - for(size_t i = 0; i < Perc->inc; i++) + for(size_t i = 0; i < Perc->Sp.inc; i++) { ae2f_float_t got = Buff[i] * Buff[i]; out_checksum += got; printf("Check-got: %f\n", got); } - out_checksum += *Perc->B(); - out_checksum = Perc->Act(out_checksum); + out_checksum += *Perc->Sp.B(); + out_checksum = Perc->Sp.Act(out_checksum); printf("Checking two values match...: %f %f\n", out_checksum, outfloat); if((out_checksum - outfloat) * (out_checksum - outfloat) > gThreshold) { printf("Check failed\n"); diff --git a/test/src/SlpTrainAND.c b/test/src/SlpTrainAND.cc similarity index 50% rename from test/src/SlpTrainAND.c rename to test/src/SlpTrainAND.cc index 3a14fc1..b321549 100644 --- a/test/src/SlpTrainAND.c +++ b/test/src/SlpTrainAND.cc @@ -19,11 +19,14 @@ static ae2f_AnnDelta_t Backward; static ae2f_float_t Backward(ae2f_float_t o, ae2f_float_t T) { - return (T - o) * ForwardPrime(o); } - +int mainc(), maincc(); int main() { + return mainc() + maincc(); +} + +int mainc() { // [1, 1], [1, 0], [0, 1], [0, 0] ae2f_float_t ins[] = { 1, 1, 1, 0, 0, 1, 0, 0 @@ -140,7 +143,167 @@ int main() { err = err2; goto __failure; } printf("Checking the value: %f\n", outbuff[0]); if(outbuff[0] > 0.5) { - err2 = ae2f_AnnSpPredict(ae2f_AnnSlpPerV(Slp, 0), ins + 6, outbuff); + err2 = ae2f_mAnnSpPredict(ae2f_AnnSlpPerV(Slp, 0), ins + 6, outbuff); + printf("Checking the value (SP): %f\n", outbuff[0]); + + printf("AND 0, 0 no good\n"); + err = ae2f_errGlob_IMP_NOT_FOUND; + } + + err2 = ae2fCL_AnnSlpPredict( + Slp, ins, 0, + 4/*in_idx*/, 0, outbuff, + queue, CL_TRUE, 0, 0, 0, context + ); if(err2) { + err = err2; goto __failure; + } printf("Checking the value: %f\n", outbuff[0]); + if(outbuff[0] > 0.5) { + printf("AND 0, 1 no good\n"); + err = ae2f_errGlob_IMP_NOT_FOUND; + } + + err2 = ae2fCL_AnnSlpPredict( + Slp, ins, 0, + 2/*in_idx*/, 0, outbuff, + queue, CL_TRUE, 0, 0, 0, context + ); if(err2) { + err = err2; goto __failure; + } printf("Checking the value: %f\n", outbuff[0]); + if(outbuff[0] > 0.5) { + printf("AND 1, 0 no good\n"); + err = ae2f_errGlob_IMP_NOT_FOUND; + } + + __failure: + ae2f_AnnSlpDel(Slp); + printf("ERR: %d\n", err); + return err; +} + + + +int maincc() { + // [1, 1], [1, 0], [0, 1], [0, 0] + ae2f_float_t ins[] = { + 1, 1, 1, 0, 0, 1, 0, 0 + }; + + + const ae2f_float_t goals[] = { + 1, 1, 0, 0 + }; + + ae2f_float_t diff_got[2]; + + ae2f_err_t err2, err; + ae2f_AnnSlp* Slp = ae2f_AnnSlpMkB( + 2, 0, 0, Forward, Backward, 1, + 0, &err2 + ); + ae2f_float_t outbuff[2] = { 5 }; + + #undef ae2fCL_AnnSlpPredict + #define ae2fCL_AnnSlpPredict(obj, inb, _, in_idx, __, out, ...) \ + (obj)->Predict(inb + in_idx, out) // ae2f_AnnSlpPredict(obj, inb + in_idx, out) + + err2 = ae2fCL_AnnSlpPredict( + Slp, ins, 0, + 0/*in_idx*/, 0, outbuff, + queue, CL_TRUE, 0, 0, 0, context + ); if(err2) { + err = err2; goto __failure; + } printf("Checking the value: %f (1, 1)\n", outbuff[0]); + + err2 = ae2fCL_AnnSlpPredict( + Slp, ins, 0, + 6/*in_idx*/, 0, outbuff, + queue, CL_TRUE, 0, 0, 0, context + ); if(err2) { + err = err2; goto __failure; + } printf("Checking the value: %f (0, 0)\n", outbuff[0]); + + err2 = ae2fCL_AnnSlpPredict( + Slp, ins, 0, + 4/*in_idx*/, 0, outbuff, + queue, CL_TRUE, 0, 0, 0, context + ); if(err2) { + err = err2; goto __failure; + } printf("Checking the value: %f (0, 1)\n", outbuff[0]); + + err2 = ae2fCL_AnnSlpPredict( + Slp, ins, 0, + 2/*in_idx*/, 0, outbuff, + queue, CL_TRUE, 0, 0, 0, context + ); if(err2) { + err = err2; goto __failure; + } printf("Checking the value: %f (1, 0)\n", outbuff[0]); + + if(err2) { + err = err2; goto __failure; + } + + #undef ae2fCL_AnnSlpTrain + #define ae2fCL_AnnSlpTrain(obj, ins, _, in_idx, __, goal, learnrate, ...) \ + (obs)->TrainB(ins + in_idx, goal, learnrate) // ae2f_AnnSlpTrainB(obj, ins + in_idx, goal, learnrate) + + if(err) goto __failure; + for(size_t _ = 0; _ < gEpochs; _++) { + err2 = ae2f_AnnSlpTrainB( + Slp, ins, + goals + 0, gLearningRate + ); + if(err2) { + err = err2; goto __failure; + } + + err2 = ae2f_AnnSlpTrainB( + Slp, ins + 2/*in_idx*/, + goals + 2, gLearningRate + ); + if(err2) { + err = err2; goto __failure; + } + + err2 = ae2f_AnnSlpTrainB( + Slp, ins + 4/*in_idx*/, + goals + 2, gLearningRate + ); + if(err2) { + err = err2; goto __failure; + } + + err2 = ae2f_AnnSlpTrainB( + Slp, ins + 6/*in_idx*/, + goals + 2, gLearningRate + ); + if(err2) { + err = err2; goto __failure; + } + } + + puts("we are going for predict"); + + err2 = ae2fCL_AnnSlpPredict( + Slp, ins, 0, + 0/*in_idx*/, 0, outbuff, + queue, CL_TRUE, 0, 0, 0, context + ); if(err2) { + err = err2; goto __failure; + } printf("Checking the value: %f\n", outbuff[0]); + if(outbuff[0] < 0.5) { + printf("AND 1, 1 no good\n"); + err = ae2f_errGlob_IMP_NOT_FOUND; + } + + err2 = ae2fCL_AnnSlpPredict( + Slp, ins, 0, + 6/*in_idx*/, 0, outbuff, + queue, CL_TRUE, 0, 0, 0, context + ); if(err2) { + err = err2; goto __failure; + } printf("Checking the value: %f\n", outbuff[0]); + if(outbuff[0] > 0.5) { + err2 = ae2f_mAnnSpPredict(ae2f_AnnSlpPerV(Slp, 0), ins + 6, outbuff); printf("Checking the value (SP): %f\n", outbuff[0]); printf("AND 0, 0 no good\n"); diff --git a/test/src/SpTrainAND.c b/test/src/SpTrainAND.c index f7d5c52..5a5ae08 100644 --- a/test/src/SpTrainAND.c +++ b/test/src/SpTrainAND.c @@ -45,18 +45,18 @@ int main() { ae2f_float_t diff_got[2]; ae2f_err_t err2, err; - ae2f_AnnSp* Slp = ae2f_AnnSpMk( + ae2f_mAnnSp* Slp = (ae2f_mAnnSp*)ae2f_mAnnSpMk( 2, 0, Forward, Backward, &err2, 0 ); if(err2) { err = err2; goto __failure; } - #define ae2fCL_AnnSlpTrain(obj, ins, _, in_idx, __, goal, learnrate, ...) ae2f_AnnSpTrainB(obj, ins + in_idx, *(goal), learnrate) + #define ae2fCL_AnnSlpTrain(obj, ins, _, in_idx, __, goal, learnrate, ...) ae2f_mAnnSpTrainB(obj, ins + in_idx, *(goal), learnrate) if(err) goto __failure; for(size_t _ = 0; _ < gEpochs; _++) { - err2 = ae2f_AnnSpTrainB( + err2 = ae2f_mAnnSpTrainB( Slp, ins, *goals + 0, gLearningRate ); @@ -100,7 +100,7 @@ int main() { ae2f_float_t outbuff[2] = { 5 }; #define ae2fCL_AnnSlpPredict(obj, inb, _, in_idx, __, out, ...) \ - ae2f_AnnSpPredict(obj, inb + in_idx, (out)) + ae2f_mAnnSpPredict(obj, inb + in_idx, (out)) err2 = ae2fCL_AnnSlpPredict( Slp, ins, 0, @@ -151,6 +151,6 @@ int main() { } __failure: - ae2f_AnnSpDel(Slp); + ae2f_mAnnSpDel(Slp); return err | err2; } \ No newline at end of file From 9e31aab54c809bd6be3735bc823768a091e5d509 Mon Sep 17 00:00:00 2001 From: ae2f Date: Thu, 13 Mar 2025 01:11:39 +0000 Subject: [PATCH 03/13] Abstraction: for non-cl --- include/ae2f/Ann/Mlp.h | 26 +++--- include/ae2f/Ann/Mlp.h.cxx/imp.hh | 9 +- include/ae2f/Ann/Mlp.h.cxx/mMlp.hh | 3 + include/ae2f/Ann/Slp.h | 134 ++++++++++++++++------------- include/ae2f/Ann/Slp.h.cxx/Slp.hh | 41 +-------- include/ae2f/Ann/Slp.h.cxx/imp.hh | 41 +++++---- include/ae2f/Ann/Slp.h.cxx/mSlp.hh | 45 ++++++++++ include/ae2f/Ann/Sp.h | 11 ++- include/ae2f/Ann/Sp.h.cxx/imp.hh | 4 - include/ae2f/Ann/Sp.h.cxx/mSp.hh | 1 - include/ae2fCL/Ann/Slp.h | 12 +-- src/ann-h/Mlp.h | 16 ++-- src/ann-h/Mlp/HidCompute.h | 8 +- src/ann-h/Mlp/HidErr.h | 4 +- src/ann-h/Mlp/OutCompute.h | 4 +- src/ann-h/Mlp/Predict.h | 4 +- src/ann-h/Slp.h | 22 ++--- src/ann/Mlp.c | 6 +- src/ann/Slp.c | 16 ++-- src/ann/Sp.c | 2 +- src/cl/Mlp.c | 2 +- src/cl/Slp.c | 30 +++---- test/cl-src/MlpTrainXOR.c | 18 ++-- test/cl-src/SlpMk.cc | 12 +-- test/cl-src/SlpTrainAND.c | 18 ++-- test/src/MlpTrainAND.c | 28 +++--- test/src/MlpTrainXOR.cc | 26 +++--- test/src/PercMk.cc | 4 +- test/src/SlpTrainAND.cc | 48 +++++------ test/src/SpTrainAND.c | 2 +- 30 files changed, 321 insertions(+), 276 deletions(-) create mode 100644 include/ae2f/Ann/Slp.h.cxx/mSlp.hh diff --git a/include/ae2f/Ann/Mlp.h b/include/ae2f/Ann/Mlp.h index 3038d81..0510a92 100644 --- a/include/ae2f/Ann/Mlp.h +++ b/include/ae2f/Ann/Mlp.h @@ -17,7 +17,7 @@ /// @brief /// Element of @ref ae2f_mAnnMlp -typedef ae2f_AnnSlp ae2f_mAnnMlpEl; +typedef ae2f_mAnnSlp ae2f_mAnnMlpEl; #if ae2f_WhenCXX(!) 0 @@ -25,23 +25,23 @@ struct ae2f_mAnnMlp { #include "Mlp.h.cxx/mMlp.hh" private: -ae2f_AnnSlp _this[1]; +ae2f_mAnnSlp _this[1]; }; #else /// @class ae2f_mAnnMlp -/// @extends ae2f_AnnSlp +/// @extends ae2f_mAnnSlp /// @brief /// # Multi Layered Perceptron /// /// -typedef ae2f_AnnSlp ae2f_mAnnMlp; +typedef ae2f_mAnnSlp ae2f_mAnnMlp; #endif typedef union ae2f_AnnMlp { - ae2f_AnnSlp Slp; + ae2f_mAnnSlp Slp; ae2f_mAnnMlp Mlp; #if ae2f_WhenCXX(!)0 @@ -50,8 +50,8 @@ typedef union ae2f_AnnMlp { } ae2f_AnnMlp; /// @memberof ae2f_mAnnMlp -/// @details ae2f_AnnSlpPerVPad -#define ae2f_mAnnMlpLayerVPad ae2f_AnnSlpPerVPad +/// @details ae2f_mAnnSlpPerVPad +#define ae2f_mAnnMlpLayerVPad ae2f_mAnnSlpPerVPad /// @memberof ae2f_mAnnMlp #define ae2f_mAnnMlpLayerV(mlp, i, ...) \ @@ -64,22 +64,22 @@ ae2f_reinterpret_cast(__VA_ARGS__ ae2f_mAnnMlpEl*, ae2f_mAnnMlpLayerVPad(mlp, __ /// @brief /// Predict function api. \n /// For details see @ref ae2f_mAnnMlpPredict_t. -typedef ae2f_AnnSlpPredict_t ae2f_mAnnMlpPredict_t; +typedef ae2f_mAnnSlpPredict_t ae2f_mAnnMlpPredict_t; /// @brief /// Training function api. \n /// For details see @ref ae2f_mAnnMlpTrain_t -typedef ae2f_AnnSlpTrain_t ae2f_mAnnMlpTrain_t; +typedef ae2f_mAnnSlpTrain_t ae2f_mAnnMlpTrain_t; /// @brief /// Cleaning function api. -typedef ae2f_AnnSlpClean_t ae2f_mAnnMlpClean_t; +typedef ae2f_mAnnSlpClean_t ae2f_mAnnMlpClean_t; /// @memberof ae2f_mAnnMlp /// @brief /// The predicted max buffer count among all perceptron's possible length of I/O. #define ae2f_mAnnMlpLayerBuffCount(mlp, ...) \ -ae2f_AnnSlpX(mlp, __VA_ARGS__ size_t*, __VA_ARGS__) +ae2f_mAnnSlpX(mlp, __VA_ARGS__ size_t*, __VA_ARGS__) /// @memberof ae2f_mAnnMlp /// @brief @@ -133,6 +133,10 @@ ae2f_AnnMlp* ae2f_AnnMlpMk( ae2f_err_t* errret_opt ) noexcept; + +#define ae2f_AnnMlpClean ae2f_AnnSlpClean +#define ae2f_AnnMlpDel ae2f_AnnSlpDel + #if ae2f_WhenCXX(!) 0 #include "Mlp.h.cxx/imp.hh" #endif diff --git a/include/ae2f/Ann/Mlp.h.cxx/imp.hh b/include/ae2f/Ann/Mlp.h.cxx/imp.hh index 973da5b..6717f7c 100644 --- a/include/ae2f/Ann/Mlp.h.cxx/imp.hh +++ b/include/ae2f/Ann/Mlp.h.cxx/imp.hh @@ -20,11 +20,18 @@ constexprfun const ae2f_mAnnMlpEl* ae2f_TMP Perc(size_t a) const { return ae2f_mAnnMlpLayerV(this->_this, a, const); } +constexprfun size_t* ae2f_TMP PercPad(size_t a) { + return ae2f_mAnnMlpLayerVPad(this->_this)[a]; +} +constexprfun const size_t* ae2f_TMP PercPad(size_t a) const { + return ae2f_mAnnMlpLayerVPad(this->_this, const)[a]; +} + #undef ae2f_TMP #define ae2f_TMP ae2f_AnnMlp:: inline ae2f_TMP ~ae2f_AnnMlp() { - ae2f_AnnSlpClean(&this->Slp); + ae2f_AnnMlpClean(this); } #undef ae2f_TMP \ No newline at end of file diff --git a/include/ae2f/Ann/Mlp.h.cxx/mMlp.hh b/include/ae2f/Ann/Mlp.h.cxx/mMlp.hh index 47a1225..ef0fcf4 100644 --- a/include/ae2f/Ann/Mlp.h.cxx/mMlp.hh +++ b/include/ae2f/Ann/Mlp.h.cxx/mMlp.hh @@ -16,4 +16,7 @@ #define ae2f_TMP constexprfun ae2f_mAnnMlpEl* ae2f_TMP Perc(size_t a); constexprfun const ae2f_mAnnMlpEl* ae2f_TMP Perc(size_t a) const; + +constexprfun size_t* ae2f_TMP PercPad(size_t); +constexprfun const size_t* ae2f_TMP PercPad(size_t) const; #undef ae2f_TMP \ No newline at end of file diff --git a/include/ae2f/Ann/Slp.h b/include/ae2f/Ann/Slp.h index 187c874..cf2b0b1 100644 --- a/include/ae2f/Ann/Slp.h +++ b/include/ae2f/Ann/Slp.h @@ -15,10 +15,10 @@ #include "./Sp.h" #include -struct ae2f_AnnSlp; +struct ae2f_mAnnSlp; /// @brief cleaning function -typedef ae2f_err_t ae2f_AnnSlpClean_t (ae2f_struct ae2f_AnnSlp*) noexcept; +typedef ae2f_err_t ae2f_mAnnSlpClean_t (ae2f_struct ae2f_mAnnSlp*) noexcept; /// @brief /// Predict one output from multiple inputs. @@ -29,8 +29,8 @@ typedef ae2f_err_t ae2f_AnnSlpClean_t (ae2f_struct ae2f_AnnSlp*) noexcept; /// That one predicted. \n /// Its count is @ref ae2f_mAnnSp::inc. /// @return State -typedef ae2f_err_t ae2f_AnnSlpPredict_t ( - const ae2f_struct ae2f_AnnSlp* _this, +typedef ae2f_err_t ae2f_mAnnSlpPredict_t ( + const ae2f_struct ae2f_mAnnSlp* _this, const ae2f_float_t* in, ae2f_float_t* outret_opt ) noexcept; @@ -47,8 +47,8 @@ typedef ae2f_err_t ae2f_AnnSlpPredict_t ( /// Desired output. /// @param learningrate /// Learning rate -typedef ae2f_err_t ae2f_AnnSlpTrain_t ( - ae2f_struct ae2f_AnnSlp* _this, +typedef ae2f_err_t ae2f_mAnnSlpTrain_t ( + ae2f_struct ae2f_mAnnSlp* _this, const ae2f_float_t* in, const ae2f_float_t* delta_optA, const ae2f_float_t* goal_optB, @@ -56,15 +56,15 @@ typedef ae2f_err_t ae2f_AnnSlpTrain_t ( ) noexcept; /// @brief -/// The element type for @ref ae2f_AnnSlp. -typedef struct ae2f_mAnnSp ae2f_AnnSlpEl; +/// The element type for @ref ae2f_mAnnSlp. +typedef struct ae2f_mAnnSp ae2f_mAnnSlpEl; /// @brief /// # Single Layered Perceptron /// /// Multiple input, multiple output. \n /// For predicting & training operations are able to be parallel. -typedef struct ae2f_AnnSlp { +typedef struct ae2f_mAnnSlp { /// @brief /// True when its shape is same as expected. \n @@ -85,80 +85,80 @@ typedef struct ae2f_AnnSlp { /// @brief /// Cleaning function - ae2f_AnnSlpClean_t* vClean; + ae2f_mAnnSlpClean_t* vClean; /// @brief /// Predict function - ae2f_AnnSlpPredict_t* vPredict; + ae2f_mAnnSlpPredict_t* vPredict; /// @brief /// Training function - ae2f_AnnSlpTrain_t* vTrain; + ae2f_mAnnSlpTrain_t* vTrain; #if ae2f_WhenCXX(!)0 - #include "Slp.h.cxx/Slp.hh" + #include "Slp.h.cxx/mSlp.hh" #endif -} ae2f_AnnSlp; +} ae2f_mAnnSlp; -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief /// Input padding for perceptron. /// /// Valid when @ref ae2f_mAnnSp::expected is 1. \n /// When not, it is null. -#define ae2f_AnnSlpPerVPad(slp, ...) \ +#define ae2f_mAnnSlpPerVPad(slp, ...) \ (ae2f_CmpGetMem(slp, expected, 0) ? \ -ae2f_reinterpret_cast(__VA_ARGS__ size_t* __VA_ARGS__ *, ae2f_static_cast(__VA_ARGS__ ae2f_AnnSlp*, slp) + 1) : \ +ae2f_reinterpret_cast(__VA_ARGS__ size_t* __VA_ARGS__ *, ae2f_static_cast(__VA_ARGS__ ae2f_mAnnSlp*, slp) + 1) : \ 0) -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief /// Perceptron vector. \n -/// Its length is same as @ref ae2f_AnnSlp::outc. +/// Its length is same as @ref ae2f_mAnnSlp::outc. /// -/// Valid when @ref ae2f_AnnSlp::expected is 1. \n +/// Valid when @ref ae2f_mAnnSlp::expected is 1. \n /// When not, it is null. -#define ae2f_AnnSlpPerV(slp, i, ...) \ -ae2f_reinterpret_cast(__VA_ARGS__ ae2f_AnnSlpEl*, (ae2f_AnnSlpPerVPad(slp, __VA_ARGS__)[i] + 1)) +#define ae2f_mAnnSlpPerV(slp, i, ...) \ +ae2f_reinterpret_cast(__VA_ARGS__ ae2f_mAnnSlpEl*, (ae2f_mAnnSlpPerVPad(slp, __VA_ARGS__)[i] + 1)) -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief /// Additional buffer allocated. -#define ae2f_AnnSlpX(slp, type, ...) \ -ae2f_reinterpret_cast(__VA_ARGS__ type, ae2f_CmpGetMem(slp, expected, 0) ? (ae2f_AnnSlpPerVPad(slp, __VA_ARGS__) + (slp)->layerc) : 0) +#define ae2f_mAnnSlpX(slp, type, ...) \ +ae2f_reinterpret_cast(__VA_ARGS__ type, ae2f_CmpGetMem(slp, expected, 0) ? (ae2f_mAnnSlpPerVPad(slp, __VA_ARGS__) + (slp)->layerc) : 0) -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief /// Desired least byte size for initialising. -#define ae2f_AnnSlpInitSz(outc, off) ((off) + sizeof(ae2f_AnnSlp) + (outc) * sizeof(void*)) +#define ae2f_mAnnSlpInitSz(outc, off) ((off) + sizeof(ae2f_mAnnSlp) + (outc) * sizeof(void*)) -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief -/// Caller of @ref ae2f_AnnSlpPredict_t. -#define ae2f_AnnSlpPredict ae2f_mAnnSpPredict +/// Caller of @ref ae2f_mAnnSlpPredict_t. +#define ae2f_mAnnSlpPredict ae2f_mAnnSpPredict -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief -/// Caller of @ref ae2f_AnnSlpTrain_t. -#define ae2f_AnnSlpTrain ae2f_mAnnSpTrain +/// Caller of @ref ae2f_mAnnSlpTrain_t. +#define ae2f_mAnnSlpTrain ae2f_mAnnSpTrain -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief -/// Caller of @ref ae2f_AnnSlpTrain_t \n +/// Caller of @ref ae2f_mAnnSlpTrain_t \n /// Train with Pre-calculated delta. -#define ae2f_AnnSlpTrainA ae2f_mAnnSpTrainA +#define ae2f_mAnnSlpTrainA ae2f_mAnnSpTrainA -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief -/// Caller of @ref ae2f_AnnSlpTrain_t \n +/// Caller of @ref ae2f_mAnnSlpTrain_t \n /// Train for desired output: goal. -#define ae2f_AnnSlpTrainB ae2f_mAnnSpTrainB +#define ae2f_mAnnSlpTrainB ae2f_mAnnSpTrainB -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief Cleaning function -#define ae2f_AnnSlpClean ae2f_mAnnSpClean +#define ae2f_mAnnSlpClean ae2f_mAnnSpClean -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief /// @param[out] _this /// Allocated raw vector. @@ -182,10 +182,10 @@ ae2f_reinterpret_cast(__VA_ARGS__ type, ae2f_CmpGetMem(slp, expected, 0) ? (ae2f /// State number when needed /// @return /// Its desired size -/// @ref ae2f_AnnSlpInitSz(outc, offset_opt) +/// @ref ae2f_mAnnSlpInitSz(outc, offset_opt) ae2f_extern ae2f_SHAREDCALL -size_t ae2f_AnnSlpInit( - ae2f_AnnSlp* _this, +size_t ae2f_mAnnSlpInit( + ae2f_mAnnSlp* _this, const size_t* incs_optA, size_t ginc_optB, const size_t* inpads_opt, @@ -197,7 +197,7 @@ size_t ae2f_AnnSlpInit( ae2f_err_t* err_opt ) noexcept; -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief /// @param[out] _this /// Allocated raw vector. @@ -219,11 +219,11 @@ size_t ae2f_AnnSlpInit( /// State number when needed /// @return /// Its desired size -/// @ref ae2f_AnnSlpInitSz(outc, offset_opt) -#define ae2f_AnnSlpInitA(_this, incs, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) \ -ae2f_AnnSlpInit(_this, incs, 0, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) +/// @ref ae2f_mAnnSlpInitSz(outc, offset_opt) +#define ae2f_mAnnSlpInitA(_this, incs, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) \ +ae2f_mAnnSlpInit(_this, incs, 0, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief /// @param[out] _this /// Allocated raw vector. @@ -245,14 +245,21 @@ ae2f_AnnSlpInit(_this, incs, 0, inpads_opt, w_opt, Act, CalDelta, outc, offset_o /// State number when needed /// @return /// Its desired size -/// @ref ae2f_AnnSlpInitSz(outc, offset_opt) -#define ae2f_AnnSlpInitB(_this, ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) \ -ae2f_AnnSlpInit(_this, 0, ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) +/// @ref ae2f_mAnnSlpInitSz(outc, offset_opt) +#define ae2f_mAnnSlpInitB(_this, ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) \ +ae2f_mAnnSlpInit(_this, 0, ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) + +typedef union ae2f_AnnSlp { + ae2f_mAnnSlp Slp; + #if ae2f_WhenCXX(!) 0 + #include "Slp.h.cxx/Slp.hh" + #endif +} ae2f_AnnSlp; /// @memberof ae2f_AnnSlp /// @brief /// Makes a typical( @ref ae2f_mAnnSp::expected ) perceptron. -/// See @ref ae2f_AnnSlpInit. +/// See @ref ae2f_mAnnSlpInit. /// /// It is heap-allocated. pass the output @ref ae2f_mAnnSpDel after use. ae2f_extern ae2f_SHAREDCALL @@ -268,34 +275,39 @@ ae2f_AnnSlp* ae2f_AnnSlpMk( ae2f_err_t* err_opt ) noexcept; -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief /// Makes a typical( @ref ae2f_mAnnSp::expected ) perceptron. -/// See @ref ae2f_AnnSlpInit. +/// See @ref ae2f_mAnnSlpInit. /// /// It is heap-allocated. pass the output @ref ae2f_mAnnSpDel after use. #define ae2f_AnnSlpMkA(incs, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) \ ae2f_AnnSlpMk(incs, 0, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief /// Makes a typical( @ref ae2f_mAnnSp::expected ) perceptron. -/// See @ref ae2f_AnnSlpInit. +/// See @ref ae2f_mAnnSlpInit. /// /// It is heap-allocated. pass the output @ref ae2f_mAnnSpDel after use. #define ae2f_AnnSlpMkB(ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) \ ae2f_AnnSlpMk(0, ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) -/// @fn ae2f_AnnSlpDel -/// @memberof ae2f_AnnSlp +/// @fn ae2f_mAnnSlpDel +/// @memberof ae2f_mAnnSlp /// @param[out] _this /// This memory will be freed. -ae2f_AnnDelDef(ae2f_AnnSlp) noexcept { +ae2f_AnnDelDef(ae2f_mAnnSlp) noexcept { #include "Sp.h.c/clean.script.h" } +#define ae2f_AnnSlpClean(prm_Slp) ae2f_mAnnSlpClean(&(prm_Slp)->Slp) +#define ae2f_AnnSlpDel(prm_Slp) ae2f_mAnnSlpDel(&(prm_Slp)->Slp) + #if ae2f_WhenCXX(!)0 #include "Slp.h.cxx/imp.hh" #endif +#include + #endif \ No newline at end of file diff --git a/include/ae2f/Ann/Slp.h.cxx/Slp.hh b/include/ae2f/Ann/Slp.h.cxx/Slp.hh index 6f3a10e..0ba859d 100644 --- a/include/ae2f/Ann/Slp.h.cxx/Slp.hh +++ b/include/ae2f/Ann/Slp.h.cxx/Slp.hh @@ -1,44 +1,11 @@ -/** - * @file Slp.hh - * @author ae2f - * @brief - * @version 0.1 - * @date 2025-02-13 - * - * @copyright Copyright (c) 2025 - * - */ - -#include "../Slp.h" - #define ae2f_TMP -inline ae2f_err_t ae2f_TMP Predict( - const ae2f_float_t* in, - ae2f_float_t* out_opt -) const noexcept; - -inline ae2f_err_t ae2f_TMP TrainA( - const ae2f_float_t* in, - const ae2f_float_t* delta_optA, - ae2f_float_t learningrate -) noexcept; - -inline ae2f_err_t ae2f_TMP TrainB( - const ae2f_float_t* in, - const ae2f_float_t* goal_optB, - ae2f_float_t learningrate -) noexcept; - inline ae2f_TMP ~ae2f_AnnSlp(); -template -constexprfun const T* ae2f_TMP X() const; - -template -constexprfun T* ae2f_TMP X(); +private: + +uint8_t _; +constexprfun ae2f_AnnSlp() : _(0) {} -constexprfun ae2f_AnnSlpEl* ae2f_TMP Perc(size_t); -constexprfun const ae2f_AnnSlpEl* ae2f_TMP Perc(size_t) const; #undef ae2f_TMP \ No newline at end of file diff --git a/include/ae2f/Ann/Slp.h.cxx/imp.hh b/include/ae2f/Ann/Slp.h.cxx/imp.hh index cfee089..34957de 100644 --- a/include/ae2f/Ann/Slp.h.cxx/imp.hh +++ b/include/ae2f/Ann/Slp.h.cxx/imp.hh @@ -11,14 +11,14 @@ #include "../Slp.h" -#pragma region ae2f_AnnSlp -#define ae2f_TMP ae2f_AnnSlp:: +#pragma region ae2f_mAnnSlp +#define ae2f_TMP ae2f_mAnnSlp:: inline ae2f_err_t ae2f_TMP Predict( const ae2f_float_t* in, ae2f_float_t* out_opt ) const noexcept { - return ae2f_AnnSlpPredict(this, in, out_opt); + return ae2f_mAnnSlpPredict(this, in, out_opt); } inline ae2f_err_t ae2f_TMP TrainA( @@ -26,7 +26,7 @@ inline ae2f_err_t ae2f_TMP TrainA( const ae2f_float_t* delta_optA, ae2f_float_t learningrate ) noexcept { - return ae2f_AnnSlpTrainA(this, in, delta_optA, learningrate); + return ae2f_mAnnSlpTrainA(this, in, delta_optA, learningrate); } inline ae2f_err_t ae2f_TMP TrainB( @@ -34,29 +34,38 @@ inline ae2f_err_t ae2f_TMP TrainB( const ae2f_float_t* goal_optB, ae2f_float_t learningrate ) noexcept { - return ae2f_AnnSlpTrainB(this, in, goal_optB, learningrate); -} - -inline ae2f_TMP ~ae2f_AnnSlp() { - ae2f_AnnSlpClean(this); + return ae2f_mAnnSlpTrainB(this, in, goal_optB, learningrate); } template constexprfun const T* ae2f_TMP X() const { - return ae2f_AnnSlpX(this, T*, const); + return ae2f_mAnnSlpX(this, T*, const); } template constexprfun T* ae2f_TMP X() { - return ae2f_AnnSlpX(this, T*); + return ae2f_mAnnSlpX(this, T*); } -constexprfun ae2f_AnnSlpEl* ae2f_TMP Perc(size_t i) { - return ae2f_AnnSlpPerV(this, i); +constexprfun ae2f_mAnnSlpEl* ae2f_TMP Perc(size_t i) { + return ae2f_mAnnSlpPerV(this, i); } -constexprfun const ae2f_AnnSlpEl* ae2f_TMP Perc(size_t i) const { - return ae2f_AnnSlpPerV(this, i, const); +constexprfun const ae2f_mAnnSlpEl* ae2f_TMP Perc(size_t i) const { + return ae2f_mAnnSlpPerV(this, i, const); +} + +constexprfun size_t* ae2f_TMP PercPad(size_t i) { + return ae2f_mAnnSlpPerVPad(this)[i]; +} +constexprfun const size_t* ae2f_TMP PercPad(size_t i) const { + return ae2f_mAnnSlpPerVPad(this, const)[i]; } #undef ae2f_TMP -#pragma endregion \ No newline at end of file +#pragma endregion + +#define ae2f_TMP ae2f_AnnSlp:: +inline ae2f_TMP ~ae2f_AnnSlp() { + ae2f_AnnSlpClean(this); +} +#undef ae2f_TMP \ No newline at end of file diff --git a/include/ae2f/Ann/Slp.h.cxx/mSlp.hh b/include/ae2f/Ann/Slp.h.cxx/mSlp.hh new file mode 100644 index 0000000..200244e --- /dev/null +++ b/include/ae2f/Ann/Slp.h.cxx/mSlp.hh @@ -0,0 +1,45 @@ +/** + * @file Slp.hh + * @author ae2f + * @brief + * @version 0.1 + * @date 2025-02-13 + * + * @copyright Copyright (c) 2025 + * + */ + +#include "../Slp.h" + +#define ae2f_TMP + +inline ae2f_err_t ae2f_TMP Predict( + const ae2f_float_t* in, + ae2f_float_t* out_opt +) const noexcept; + +inline ae2f_err_t ae2f_TMP TrainA( + const ae2f_float_t* in, + const ae2f_float_t* delta_optA, + ae2f_float_t learningrate +) noexcept; + +inline ae2f_err_t ae2f_TMP TrainB( + const ae2f_float_t* in, + const ae2f_float_t* goal_optB, + ae2f_float_t learningrate +) noexcept; + +template +constexprfun const T* ae2f_TMP X() const; + +template +constexprfun T* ae2f_TMP X(); + +constexprfun ae2f_mAnnSlpEl* ae2f_TMP Perc(size_t); +constexprfun const ae2f_mAnnSlpEl* ae2f_TMP Perc(size_t) const; + +constexprfun size_t* ae2f_TMP PercPad(size_t); +constexprfun const size_t* ae2f_TMP PercPad(size_t) const; + +#undef ae2f_TMP \ No newline at end of file diff --git a/include/ae2f/Ann/Sp.h b/include/ae2f/Ann/Sp.h index cdf814d..1684b4c 100644 --- a/include/ae2f/Ann/Sp.h +++ b/include/ae2f/Ann/Sp.h @@ -173,14 +173,14 @@ typedef union ae2f_AnnSp { #endif } ae2f_AnnSp; -/// @memberof ae2f_mAnnSp +/// @memberof ae2f_AnnSp /// @brief /// Makes a typical( @ref ae2f_mAnnSp::expected ) perceptron. /// See @ref ae2f_mAnnSpInit. /// /// It is heap-allocated. pass the output @ref ae2f_mAnnSpDel after use. ae2f_extern ae2f_SHAREDCALL -ae2f_AnnSp* ae2f_mAnnSpMk( +ae2f_AnnSp* ae2f_AnnSpMk( size_t inum, const ae2f_float_t* W_opt, ae2f_fpAnnAct_t Act, @@ -200,14 +200,17 @@ ae2f_AnnSp* ae2f_mAnnSpMk( /// Clean the class and it will try to free the pointer. /// @param[out] _this /// Its memory will be freed. -/// @name ae2f_AnnSlpDel -/// @memberof ae2f_AnnSlp +/// @name ae2f_mAnnSlpDel +/// @memberof ae2f_mAnnSlp /// @param[out] _this /// This memory will be freed. ae2f_AnnDelDef(ae2f_mAnnSp) noexcept { #include "Sp.h.c/clean.script.h" } +#define ae2f_AnnSpClean(prm_Sp) ae2f_mAnnSpClean(&((prm_Sp)->Sp)) +#define ae2f_AnnSpDel(prm_Sp) ae2f_mAnnSpDel(&((prm_Sp)->Sp)) + /// @memberof ae2f_mAnnSp /// @brief /// Caller of @ref ae2f_mAnnSpPredict_t diff --git a/include/ae2f/Ann/Sp.h.cxx/imp.hh b/include/ae2f/Ann/Sp.h.cxx/imp.hh index 0c7ec4d..6115bb2 100644 --- a/include/ae2f/Ann/Sp.h.cxx/imp.hh +++ b/include/ae2f/Ann/Sp.h.cxx/imp.hh @@ -60,10 +60,6 @@ constexprfun T* ae2f_TMP X() { return ae2f_mAnnSpX(this, T*, ); } -inline ae2f_TMP ~ae2f_mAnnSp() { - ae2f_mAnnSpClean(this); -} - constexprfun size_t ae2f_TMP sz(size_t off, size_t inc) { return ae2f_mAnnSpInitSz(off, inc); } diff --git a/include/ae2f/Ann/Sp.h.cxx/mSp.hh b/include/ae2f/Ann/Sp.h.cxx/mSp.hh index 9b74004..ffa5610 100644 --- a/include/ae2f/Ann/Sp.h.cxx/mSp.hh +++ b/include/ae2f/Ann/Sp.h.cxx/mSp.hh @@ -37,7 +37,6 @@ template constexprfun T* ae2f_TMP X(); static constexprfun size_t ae2f_TMP sz(size_t off, size_t inc); -inline ae2f_TMP ~ae2f_mAnnSp(); constexprfun const ae2f_float_t* ae2f_TMP B() const; constexprfun ae2f_float_t* ae2f_TMP B(); diff --git a/include/ae2fCL/Ann/Slp.h b/include/ae2fCL/Ann/Slp.h index e9b0f11..455217a 100644 --- a/include/ae2fCL/Ann/Slp.h +++ b/include/ae2fCL/Ann/Slp.h @@ -20,7 +20,7 @@ ae2f_extern ae2f_SHAREDCALL size_t ae2fCL_AnnSlpInit( - ae2f_AnnSlp* _this, + ae2f_mAnnSlp* _this, const size_t* incs_optA, size_t ginc_optB, const size_t* inpads_opt, @@ -41,7 +41,7 @@ ae2fCL_AnnSlpInit(_this, 0, ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset #if ae2f_WhenCXX(!) 0 -struct ae2fCL_AnnSlp : ae2f_AnnSlp { +struct ae2fCL_AnnSlp : ae2f_mAnnSlp { #include "Slp.h.cxx/Slp.hh" }; #else @@ -51,11 +51,11 @@ struct ae2fCL_AnnSlp : ae2f_AnnSlp { * @brief * SLP with OpenCL acceleration. */ -typedef ae2f_AnnSlp ae2fCL_AnnSlp; +typedef ae2f_mAnnSlp ae2fCL_AnnSlp; #endif ae2f_extern ae2f_SHAREDCALL -ae2f_AnnSlp* ae2fCL_AnnSlpMk( +ae2f_mAnnSlp* ae2fCL_AnnSlpMk( const size_t* incs_optA, size_t ginc_optB, const size_t* inpads_opt, @@ -86,11 +86,11 @@ typedef struct ae2fCL_AnnSlpMemX { * @param off * */ #define ae2fCL_AnnSlpInitSz(outc, off) \ - ae2f_AnnSlpInitSz(outc, (off) + sizeof(ae2fCL_AnnSlpMemX) + (sizeof(ae2f_float_t) * outc)) + ae2f_mAnnSlpInitSz(outc, (off) + sizeof(ae2fCL_AnnSlpMemX) + (sizeof(ae2f_float_t) * outc)) #define ae2fCL_AnnSlpAdd(slp, ...) \ - ae2f_AnnSlpX(slp, ae2fCL_AnnSlpMemX*, __VA_ARGS__) + ae2f_mAnnSlpX(slp, ae2fCL_AnnSlpMemX*, __VA_ARGS__) #define ae2fCL_AnnSlpOutCache(slp, ...) \ ae2f_reinterpret_cast(__VA_ARGS__ ae2f_float_t*, ae2fCL_AnnSlpAdd(slp, __VA_ARGS__) + 1) diff --git a/src/ann-h/Mlp.h b/src/ann-h/Mlp.h index 7b93264..86f5842 100644 --- a/src/ann-h/Mlp.h +++ b/src/ann-h/Mlp.h @@ -23,7 +23,7 @@ static ae2f_mAnnMlpTrain_t Train; static ae2f_mAnnMlpClean_t Clean; static ae2f_err_t Predict ( - const ae2f_AnnSlp* _this, + const ae2f_mAnnSlp* _this, const ae2f_float_t* in, ae2f_float_t* outret_opt ) noexcept { @@ -46,11 +46,11 @@ static ae2f_err_t Predict ( ae2f_err_t err = 0; for(size_t i = 0; i < _this->layerc; i++) { - const ae2f_AnnSlp* slp = ae2f_mAnnMlpLayerV(_this, i, const); + const ae2f_mAnnSlp* slp = ae2f_mAnnMlpLayerV(_this, i, const); if(i == _this->layerc - 1) tmpVo.F = outret_opt; - err |= ae2f_AnnSlpPredict( + err |= ae2f_mAnnSlpPredict( slp, tmpVi.CF, tmpVo.F ); @@ -62,7 +62,7 @@ static ae2f_err_t Predict ( } static ae2f_err_t Train ( - ae2f_AnnSlp* _this, + ae2f_mAnnSlp* _this, const ae2f_float_t* in, const ae2f_float_t* delta_optA, const ae2f_float_t* goal_optB, @@ -92,7 +92,7 @@ static ae2f_err_t Train ( for(size_t i = _this->layerc - 1; i != ((size_t)-1); i--) { - ae2f_AnnSlp + ae2f_mAnnSlp * const LAYER = ae2f_mAnnMlpLayerV(_this, i), * const LAYERNXT = ae2f_mAnnMlpLayerV(_this, i + 1); @@ -121,7 +121,7 @@ static ae2f_err_t Train ( ); } - ret |= ae2f_AnnSlpTrainA( + ret |= ae2f_mAnnSlpTrainA( LAYER, i ? cache_Out2 + MAXBUFFCOUNT_FOR_LAYER * (i - 1) : in, cache_Deltas + (i) * MAXBUFFCOUNT_FOR_LAYER, @@ -146,11 +146,11 @@ static ae2f_err_t Clean( union { size_t* pad; - ae2f_AnnSlp* slp; + ae2f_mAnnSlp* slp; }* u; } perc = { .unused = ae2f_mAnnMlpLayerVPad(_this) + i }; perc.u->pad++; - ae2f_AnnSlpClean(perc.u->slp); + ae2f_mAnnSlpClean(perc.u->slp); free(--perc.u->pad); ae2f_mAnnMlpLayerVPad(_this, )[i] = 0; } diff --git a/src/ann-h/Mlp/HidCompute.h b/src/ann-h/Mlp/HidCompute.h index 0f4e187..b197c8b 100644 --- a/src/ann-h/Mlp/HidCompute.h +++ b/src/ann-h/Mlp/HidCompute.h @@ -1,14 +1,14 @@ #include static ae2f_float_t MlpTrain_HidErr( - const ae2f_AnnSlp* layerNxt, + const ae2f_mAnnSlp* layerNxt, const ae2f_float_t* deltasNxt, size_t idxThen ); static void MlpTrain_HidCompute( - const ae2f_AnnSlp* layerThen, - const ae2f_AnnSlp* layerNxt, + const ae2f_mAnnSlp* layerThen, + const ae2f_mAnnSlp* layerNxt, ae2f_float_t* retDeltaThen, const ae2f_float_t* deltasNxt, const ae2f_float_t* outThen @@ -16,7 +16,7 @@ static void MlpTrain_HidCompute( for(size_t i = 0; i < layerThen->outc; i++) { const ae2f_float_t err = MlpTrain_HidErr(layerNxt, deltasNxt, i); - retDeltaThen[i] = ae2f_AnnSlpPerV(layerThen, i, const)->CalDelta( + retDeltaThen[i] = ae2f_mAnnSlpPerV(layerThen, i, const)->CalDelta( outThen[i], err + outThen[i] ); diff --git a/src/ann-h/Mlp/HidErr.h b/src/ann-h/Mlp/HidErr.h index 171d848..5f77048 100644 --- a/src/ann-h/Mlp/HidErr.h +++ b/src/ann-h/Mlp/HidErr.h @@ -7,7 +7,7 @@ /// /// Sum for multiplying Delta and Weight static ae2f_float_t MlpTrain_HidErr( - const ae2f_AnnSlp* layerNxt, + const ae2f_mAnnSlp* layerNxt, const ae2f_float_t* deltasNxt, size_t idxThen ) { @@ -16,7 +16,7 @@ static ae2f_float_t MlpTrain_HidErr( union { const size_t* pad; const ae2f_mAnnSp* slp; - } perv = { .pad = ae2f_AnnSlpPerVPad(layerNxt)[i] + 1 }; + } perv = { .pad = ae2f_mAnnSlpPerVPad(layerNxt)[i] + 1 }; ret += ae2f_mAnnSpW(perv.slp, const)[idxThen] * deltasNxt[i]; } diff --git a/src/ann-h/Mlp/OutCompute.h b/src/ann-h/Mlp/OutCompute.h index ee34449..6391cd0 100644 --- a/src/ann-h/Mlp/OutCompute.h +++ b/src/ann-h/Mlp/OutCompute.h @@ -1,13 +1,13 @@ #include static void MlpTrain_OutCompute( - const ae2f_AnnSlp* layerOut, + const ae2f_mAnnSlp* layerOut, const ae2f_float_t* goal, const ae2f_float_t* out, ae2f_float_t* retDeltaOut ) { for(size_t i = 0; i < layerOut->outc; i++) { - retDeltaOut[i] = ae2f_AnnSlpPerV(layerOut, i, const)->CalDelta( + retDeltaOut[i] = ae2f_mAnnSlpPerV(layerOut, i, const)->CalDelta( out[i], goal[i] ); } diff --git a/src/ann-h/Mlp/Predict.h b/src/ann-h/Mlp/Predict.h index 6ec9adf..be53003 100644 --- a/src/ann-h/Mlp/Predict.h +++ b/src/ann-h/Mlp/Predict.h @@ -23,9 +23,9 @@ static ae2f_err_t MlpTrain_Predict( ae2f_err_t err = 0; for(size_t i = 0; i < _this->layerc; i++) { - const ae2f_AnnSlp* slp = ae2f_mAnnMlpLayerV(_this, i, const); + const ae2f_mAnnSlp* slp = ae2f_mAnnMlpLayerV(_this, i, const); - err |= ae2f_AnnSlpPredict( + err |= ae2f_mAnnSlpPredict( slp, tmpVi.CF, tmpVo.F ); diff --git a/src/ann-h/Slp.h b/src/ann-h/Slp.h index 510c660..9f55aae 100644 --- a/src/ann-h/Slp.h +++ b/src/ann-h/Slp.h @@ -2,12 +2,12 @@ #include #include -static ae2f_AnnSlpPredict_t Predict; -static ae2f_AnnSlpTrain_t Train; -static ae2f_AnnSlpClean_t Clean; +static ae2f_mAnnSlpPredict_t Predict; +static ae2f_mAnnSlpTrain_t Train; +static ae2f_mAnnSlpClean_t Clean; static ae2f_err_t Predict( - const ae2f_AnnSlp* _this, + const ae2f_mAnnSlp* _this, const ae2f_float_t* in, ae2f_float_t* out_opt ) noexcept { @@ -21,7 +21,7 @@ static ae2f_err_t Predict( union { const size_t* pad; const ae2f_mAnnSp* perc; - } layer = {ae2f_AnnSlpPerVPad(_this, const)[i]}; + } layer = {ae2f_mAnnSlpPerVPad(_this, const)[i]}; size_t _pad = *layer.pad; layer.pad++; @@ -40,7 +40,7 @@ static ae2f_err_t Predict( } static ae2f_err_t Train( - ae2f_AnnSlp* _this, + ae2f_mAnnSlp* _this, const ae2f_float_t* in, const ae2f_float_t* delta_optA, const ae2f_float_t* goal_optB, @@ -58,7 +58,7 @@ static ae2f_err_t Train( union { size_t* pad; ae2f_mAnnSp* perc; - } layer = {ae2f_AnnSlpPerVPad(_this)[i]}; + } layer = {ae2f_mAnnSlpPerVPad(_this)[i]}; size_t _pad = *layer.pad; layer.pad++; @@ -78,7 +78,7 @@ static ae2f_err_t Train( union { size_t* pad; ae2f_mAnnSp* perc; - } layer = {ae2f_AnnSlpPerVPad(_this)[i]}; + } layer = {ae2f_mAnnSlpPerVPad(_this)[i]}; size_t _pad = *layer.pad; layer.pad++; @@ -95,7 +95,7 @@ static ae2f_err_t Train( } static ae2f_err_t Clean( - ae2f_AnnSlp* _this + ae2f_mAnnSlp* _this ) noexcept { if(!_this) return ae2f_errGlob_PTR_IS_NULL; @@ -103,11 +103,11 @@ static ae2f_err_t Clean( const union { ae2f_mAnnSp* v; size_t* S; - } per = { .v = ae2f_AnnSlpPerV(_this, i)}; + } per = { .v = ae2f_mAnnSlpPerV(_this, i)}; ae2f_mAnnSpClean(per.v); free(per.S - 1); - ae2f_AnnSlpPerVPad(_this, )[i] = 0; + ae2f_mAnnSlpPerVPad(_this, )[i] = 0; } _this->inc = 0; diff --git a/src/ann/Mlp.c b/src/ann/Mlp.c index 0f1b58f..73ad77f 100644 --- a/src/ann/Mlp.c +++ b/src/ann/Mlp.c @@ -43,19 +43,19 @@ size_t ae2f_mAnnMlpInit( union { size_t* pad; - ae2f_AnnSlp* slp; + ae2f_mAnnSlp* slp; }* u; } perc = { ae2f_mAnnMlpLayerVPad(_this) + i }; perc.u->pad = calloc( - ae2f_AnnSlpInitSz(LAYERSZ_R, sizeof(size_t)), + ae2f_mAnnSlpInitSz(LAYERSZ_R, sizeof(size_t)), 1 ); perc.u->pad++; - ae2f_AnnSlpInitB( + ae2f_mAnnSlpInitB( perc.u->slp, LAYERSZ_L, 0, weights_opt, actglob_opt, deltaglob_opt, diff --git a/src/ann/Slp.c b/src/ann/Slp.c index acfe19e..5c0335f 100644 --- a/src/ann/Slp.c +++ b/src/ann/Slp.c @@ -1,8 +1,8 @@ #include ae2f_SHAREDEXPORT -size_t ae2f_AnnSlpInit( - ae2f_AnnSlp* _this, +size_t ae2f_mAnnSlpInit( + ae2f_mAnnSlp* _this, const size_t* incs_optA, size_t ginc_optB, const size_t* inpads_opt, @@ -35,7 +35,7 @@ size_t ae2f_AnnSlpInit( _inc = incs_optA ? incs_optA[i] : ginc_optB, _pad = inpads_opt ? inpads_opt[i] : 0; - if(!(ae2f_AnnSlpPerVPad(_this)[i] + if(!(ae2f_mAnnSlpPerVPad(_this)[i] = calloc(ae2f_mAnnSpInitSz(sizeof(size_t), _inc), 1))) { er |= ae2f_errGlob_ALLOC_FAILED; @@ -43,14 +43,14 @@ size_t ae2f_AnnSlpInit( } ae2f_mAnnSpInit( - ae2f_AnnSlpPerV(_this, i), + ae2f_mAnnSlpPerV(_this, i), _inc, w_opt, Act, CalDelta, &ertmp, 0 ); er |= ertmp; - *(ae2f_AnnSlpPerVPad(_this)[i]) = _pad; + *(ae2f_mAnnSlpPerVPad(_this)[i]) = _pad; w_opt && (w_opt += _inc); @@ -65,7 +65,7 @@ size_t ae2f_AnnSlpInit( #undef return DONE: if(err) *err = er; - return ae2f_AnnSlpInitSz(outc, offset_opt); + return ae2f_mAnnSlpInitSz(outc, offset_opt); } ae2f_SHAREDEXPORT @@ -81,8 +81,8 @@ ae2f_AnnSlp* ae2f_AnnSlpMk( ae2f_err_t* err ) noexcept { ae2f_AnnSlp* _this = 0; - _this = calloc(ae2f_AnnSlpInitSz(outc, offset_opt), 1); - ae2f_AnnSlpInit(_this, incs_optA, ginc_optB, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err); + _this = calloc(ae2f_mAnnSlpInitSz(outc, offset_opt), 1); + ae2f_mAnnSlpInit(&_this->Slp, incs_optA, ginc_optB, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err); if(err) *err &= ~ae2f_errGlob_DONE_HOWEV; return _this; } diff --git a/src/ann/Sp.c b/src/ann/Sp.c index 1c178a6..cc5f86d 100644 --- a/src/ann/Sp.c +++ b/src/ann/Sp.c @@ -66,7 +66,7 @@ size_t ae2f_mAnnSpInit( } ae2f_SHAREDEXPORT -ae2f_AnnSp* ae2f_mAnnSpMk( +ae2f_AnnSp* ae2f_AnnSpMk( size_t inc, const ae2f_float_t* W_opt, ae2f_fpAnnAct_t Act, diff --git a/src/cl/Mlp.c b/src/cl/Mlp.c index ec21b2f..9d57f01 100644 --- a/src/cl/Mlp.c +++ b/src/cl/Mlp.c @@ -49,7 +49,7 @@ size_t ae2fCL_AnnMlpInit( union { size_t* pad; - ae2f_AnnSlp* slp; + ae2f_mAnnSlp* slp; }* u; } perc = {ae2f_mAnnMlpLayerVPad(_this) + i}; diff --git a/src/cl/Slp.c b/src/cl/Slp.c index 47eb12e..5e6e661 100644 --- a/src/cl/Slp.c +++ b/src/cl/Slp.c @@ -13,7 +13,7 @@ ae2f_err_t TrainCL( - ae2f_AnnSlp* _this, + ae2f_mAnnSlp* _this, const ae2f_float_t* in, const ae2f_float_t* delta_optA, const ae2f_float_t* goal_optB, @@ -87,7 +87,7 @@ ae2f_err_t TrainCL( { for(size_t i = 0; i < _this->outc; i++) { const size_t - * _padv = ae2f_AnnSlpPerVPad(_this, const)[i], + * _padv = ae2f_mAnnSlpPerVPad(_this, const)[i], pad = *_padv; const ae2f_mAnnSp* @@ -133,7 +133,7 @@ ae2f_err_t TrainCL( _BUFFSET: for(size_t i = 0; i < OC; i++) { - ae2f_mAnnSp* perc = ae2f_AnnSlpPerV(_this, i); + ae2f_mAnnSp* perc = ae2f_mAnnSlpPerV(_this, i); *ae2f_mAnnSpB(perc) += PREDICTED_BUFF[i] = delta_optA ? @@ -190,7 +190,7 @@ ae2f_err_t TrainCL( #if 1 for(size_t i = 0; i < _this->outc; i++) { size_t - * padv = ae2f_AnnSlpPerVPad(_this)[i] + * padv = ae2f_mAnnSlpPerVPad(_this)[i] , pad = *padv; ae2f_mAnnSp* perc = ae2f_reinterpret_cast(ae2f_mAnnSp*, padv + 1); @@ -228,7 +228,7 @@ ae2f_err_t TrainCL( * OpenCL Code for Predicting SLP. * */ ae2f_err_t PredictCL( - const ae2f_AnnSlp* _ + const ae2f_mAnnSlp* _ , const ae2f_float_t* in , ae2f_float_t* out ) @@ -287,7 +287,7 @@ ae2f_err_t PredictCL( { for(size_t i = 0; i < _->outc; i++) { const size_t - * _padv = ae2f_AnnSlpPerVPad(_, const)[i], + * _padv = ae2f_mAnnSlpPerVPad(_, const)[i], pad = *_padv; const ae2f_mAnnSp* @@ -369,7 +369,7 @@ ae2f_err_t PredictCL( for(size_t i = 0; i < OC; i++) { const ae2f_mAnnSp* - perc = ae2f_AnnSlpPerV(_,i); + perc = ae2f_mAnnSlpPerV(_,i); if(!perc) return(ae2f_errGlob_IMP_NOT_FOUND); if(!perc->Act) return(ae2f_errGlob_IMP_NOT_FOUND); @@ -390,11 +390,11 @@ ae2f_err_t PredictCL( } -static ae2f_AnnSlpClean_t CleanCL; +static ae2f_mAnnSlpClean_t CleanCL; ae2f_SHAREDEXPORT size_t ae2fCL_AnnSlpInit( - ae2f_AnnSlp* _this, + ae2f_mAnnSlp* _this, const size_t* incs_optA, size_t ginc_optB, const size_t* inpads_opt, @@ -429,7 +429,7 @@ size_t ae2fCL_AnnSlpInit( _inc = incs_optA ? incs_optA[i] : ginc_optB, _pad = inpads_opt ? inpads_opt[i] : 0; - ae2f_AnnSlpPerVPad(_this)[i] + ae2f_mAnnSlpPerVPad(_this)[i] = calloc( ae2f_mAnnSpInitSz( sizeof(size_t), _inc @@ -437,7 +437,7 @@ size_t ae2fCL_AnnSlpInit( ); ae2f_mAnnSpInit( - ae2f_AnnSlpPerV(_this, i), + ae2f_mAnnSlpPerV(_this, i), _inc, w_opt, Act, CalDelta, &ertmp, 0 @@ -446,7 +446,7 @@ size_t ae2fCL_AnnSlpInit( if(v2 != CL_SUCCESS) V = v2; er |= ertmp; - *ae2f_AnnSlpPerVPad(_this)[i] = _pad; + *ae2f_mAnnSlpPerVPad(_this)[i] = _pad; w_opt && (w_opt += _inc); @@ -474,7 +474,7 @@ size_t ae2fCL_AnnSlpInit( return ae2fCL_AnnSlpInitSz(outc, offset_opt); } -static ae2f_err_t CleanCL(ae2f_AnnSlp* _) { +static ae2f_err_t CleanCL(ae2f_mAnnSlp* _) { ae2f_err_t e = 0; if(_ && ae2fCL_AnnSlpAdd(_)->In) { if((ae2fCL_Ann.LErr = clReleaseMemObject( @@ -491,7 +491,7 @@ static ae2f_err_t CleanCL(ae2f_AnnSlp* _) { } ae2f_SHAREDEXPORT -ae2f_AnnSlp* ae2fCL_AnnSlpMk( +ae2f_mAnnSlp* ae2fCL_AnnSlpMk( const size_t* incs_optA, size_t ginc_optB, const size_t* inpads_opt, @@ -503,7 +503,7 @@ ae2f_AnnSlp* ae2fCL_AnnSlpMk( ae2f_err_t* err_opt, cl_int* err_nfound_opt ) noexcept { - ae2f_AnnSlp* rtn = calloc(ae2fCL_AnnSlpInitSz(outc, offset_opt), 1); + ae2f_mAnnSlp* rtn = calloc(ae2fCL_AnnSlpInitSz(outc, offset_opt), 1); ae2f_err_t err = 0; cl_int err2 = 0; diff --git a/test/cl-src/MlpTrainXOR.c b/test/cl-src/MlpTrainXOR.c index 7e2e8d7..47e5422 100644 --- a/test/cl-src/MlpTrainXOR.c +++ b/test/cl-src/MlpTrainXOR.c @@ -71,7 +71,7 @@ int main() { goto __failure; } for(size_t _ = 0; _ < gEpochs; _++) { - err2 = ae2f_AnnSlpTrainB( + err2 = ae2f_mAnnSlpTrainB( &Mlp->Slp, ins, goals + 2, gLearningRate ); @@ -79,7 +79,7 @@ int main() { err_ae2f = err2; goto __failure; } - err2 = ae2f_AnnSlpTrainB( + err2 = ae2f_mAnnSlpTrainB( &Mlp->Slp, ins + 2, goals, gLearningRate ); @@ -87,7 +87,7 @@ int main() { goto __failure; } - err2 = ae2f_AnnSlpTrainB( + err2 = ae2f_mAnnSlpTrainB( &Mlp->Slp, ins + 4, goals, gLearningRate ); @@ -95,7 +95,7 @@ int main() { goto __failure; } - err2 = ae2f_AnnSlpTrainB( + err2 = ae2f_mAnnSlpTrainB( &Mlp->Slp, ins + 6, goals + 2, gLearningRate ); @@ -108,7 +108,7 @@ int main() { #if 1 puts("Predict time"); - err2 = ae2f_AnnSlpPredict( + err2 = ae2f_mAnnSlpPredict( &Mlp->Slp, ins, outbuff ); if(err2) { goto __failure; @@ -118,7 +118,7 @@ int main() { err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnSlpPredict( + err2 = ae2f_mAnnSlpPredict( &Mlp->Slp, ins + 6, outbuff ); if(err2) { err2 = err2; goto __failure; @@ -128,7 +128,7 @@ int main() { err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnSlpPredict( + err2 = ae2f_mAnnSlpPredict( &Mlp->Slp, ins + 4, outbuff ); if(err2) { err_ae2f = err2; goto __failure; @@ -138,7 +138,7 @@ int main() { err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnSlpPredict( + err2 = ae2f_mAnnSlpPredict( &Mlp->Slp, ins + 2, outbuff ); if(err2) { err_ae2f = err2; goto __failure; @@ -151,7 +151,7 @@ int main() { #endif __failure: - ae2f_AnnSlpDel(&Mlp->Slp); + ae2f_mAnnSlpDel(&Mlp->Slp); ae2fCL_AnnDel(); printf("Something is over, code: %d\n", err_ae2f | err2); return err_ae2f | err2; diff --git a/test/cl-src/SlpMk.cc b/test/cl-src/SlpMk.cc index 14e5be7..2e3504d 100644 --- a/test/cl-src/SlpMk.cc +++ b/test/cl-src/SlpMk.cc @@ -24,7 +24,7 @@ int mainc() { }; ae2f_mAnnSp* Perc = 0; - ae2f_AnnSlp* SLP; + ae2f_mAnnSlp* SLP; ae2f_float_t out_checksum = 0; err = ae2fCL_AnnMkEasy(errcl); @@ -36,7 +36,7 @@ int mainc() { &err, errcl ); - Perc = ae2f_AnnSlpPerV(SLP, 0); + Perc = ae2f_mAnnSlpPerV(SLP, 0); CHECK_ERR(err, CL_SUCCESS, __failure); err = SLP->Predict(Buff, &outfloat); @@ -65,7 +65,7 @@ int mainc() { } __failure: - if(SLP) ae2f_AnnSlpDel(SLP); + if(SLP) ae2f_mAnnSlpDel(SLP); ae2fCL_AnnDel(); if(ae2fCL_Ann.Q) clReleaseCommandQueue(ae2fCL_Ann.Q); if(ae2fCL_Ann.Ctx) clReleaseContext(ae2fCL_Ann.Ctx); @@ -81,7 +81,7 @@ int maincc() { }; ae2f_mAnnSp* Perc = 0; - ae2f_AnnSlp* SLP; + ae2f_mAnnSlp* SLP; ae2f_float_t out_checksum = 0; err = ae2fCL_AnnMkEasy(errcl); @@ -93,7 +93,7 @@ int maincc() { &err, errcl ); - Perc = ae2f_AnnSlpPerV(SLP, 0); + Perc = ae2f_mAnnSlpPerV(SLP, 0); CHECK_ERR(err, CL_SUCCESS, __failure); err = SLP->Predict(Buff, &outfloat); @@ -122,7 +122,7 @@ int maincc() { } __failure: - if(SLP) ae2f_AnnSlpDel(SLP); + if(SLP) ae2f_mAnnSlpDel(SLP); ae2fCL_AnnDel(); if(ae2fCL_Ann.Q) clReleaseCommandQueue(ae2fCL_Ann.Q); if(ae2fCL_Ann.Ctx) clReleaseContext(ae2fCL_Ann.Ctx); diff --git a/test/cl-src/SlpTrainAND.c b/test/cl-src/SlpTrainAND.c index 0913742..1e60dab 100644 --- a/test/cl-src/SlpTrainAND.c +++ b/test/cl-src/SlpTrainAND.c @@ -42,14 +42,14 @@ int main() { err = ae2fCL_AnnMkEasy(&errcl); CHECK_ERR(err, CL_SUCCESS, __failure); - ae2f_AnnSlp* Slp = ae2fCL_AnnSlpMkB( + ae2f_mAnnSlp* Slp = ae2fCL_AnnSlpMkB( 2, 0, 0, Forward, Backward, 1, 0, &err2, &errcl ); ae2f_float_t outbuff[2] = { 5 }; #define ae2fCL_AnnSlpPredict(obj, inb, _, in_idx, __, out, ...) \ - ae2f_AnnSlpPredict(obj, inb + in_idx, out) + ae2f_mAnnSlpPredict(obj, inb + in_idx, out) err2 = ae2fCL_AnnSlpPredict( Slp, ins, 0, @@ -86,11 +86,11 @@ int main() { if(err2) { err = err2; goto __failure; } - #define ae2fCL_AnnSlpTrain(obj, ins, _, in_idx, __, goal, learnrate, ...) ae2f_AnnSlpTrainB(obj, ins + in_idx, goal, learnrate) + #define ae2fCL_AnnSlpTrain(obj, ins, _, in_idx, __, goal, learnrate, ...) ae2f_mAnnSlpTrainB(obj, ins + in_idx, goal, learnrate) if(err) goto __failure; for(size_t _ = 0; _ < gEpochs; _++) { - err2 = ae2f_AnnSlpTrainB( + err2 = ae2f_mAnnSlpTrainB( Slp, ins, goals + 0, gLearningRate ); @@ -101,7 +101,7 @@ int main() { goto __failure; } - err2 = ae2f_AnnSlpTrainB( + err2 = ae2f_mAnnSlpTrainB( Slp, ins + 2/*in_idx*/, goals + 2, gLearningRate ); @@ -109,7 +109,7 @@ int main() { err = err2; goto __failure; } - err2 = ae2f_AnnSlpTrainB( + err2 = ae2f_mAnnSlpTrainB( Slp, ins + 4/*in_idx*/, goals + 2, gLearningRate ); @@ -117,7 +117,7 @@ int main() { err = err2; goto __failure; } - err2 = ae2f_AnnSlpTrainB( + err2 = ae2f_mAnnSlpTrainB( Slp, ins + 6/*in_idx*/, goals + 2, gLearningRate ); @@ -148,7 +148,7 @@ int main() { err = err2; goto __failure; } printf("Checking the value: %f\n", outbuff[0]); if(outbuff[0] > 0.5) { - err2 = ae2f_mAnnSpPredict(ae2f_AnnSlpPerV(Slp, 0), ins + 6, outbuff); + err2 = ae2f_mAnnSpPredict(ae2f_mAnnSlpPerV(Slp, 0), ins + 6, outbuff); printf("Checking the value (SP): %f\n", outbuff[0]); printf("AND 0, 0 no good\n"); @@ -180,7 +180,7 @@ int main() { } __failure: - ae2f_AnnSlpDel(Slp); + ae2f_mAnnSlpDel(Slp); ae2fCL_AnnDel(); printf("ERR: %d\n", err); return err; diff --git a/test/src/MlpTrainAND.c b/test/src/MlpTrainAND.c index 26320a5..fbe3241 100644 --- a/test/src/MlpTrainAND.c +++ b/test/src/MlpTrainAND.c @@ -61,7 +61,7 @@ int main() { goto __failure; } for(size_t _ = 0; _ < gEpochs; _++) { - err2 = ae2f_AnnSlpTrainB( + err2 = ae2f_mAnnSlpTrainB( &Mlp->Slp, ins, goals + 0, gLearningRate ); @@ -69,7 +69,7 @@ int main() { err_ae2f = err2; goto __failure; } - err2 = ae2f_AnnSlpTrainB( + err2 = ae2f_mAnnSlpTrainB( &Mlp->Slp, ins + 2, goals + 2, gLearningRate ); @@ -77,7 +77,7 @@ int main() { goto __failure; } - err2 = ae2f_AnnSlpTrainB( + err2 = ae2f_mAnnSlpTrainB( &Mlp->Slp, ins + 4, goals + 2, gLearningRate ); @@ -85,7 +85,7 @@ int main() { goto __failure; } - err2 = ae2f_AnnSlpTrainB( + err2 = ae2f_mAnnSlpTrainB( &Mlp->Slp, ins + 6, goals + 2, gLearningRate ); @@ -98,7 +98,7 @@ int main() { #if 1 puts("Predict time"); - err2 = ae2f_AnnSlpPredict( + err2 = ae2f_mAnnSlpPredict( &Mlp->Mlp, ins, outbuff ); if(err2) { goto __failure; @@ -108,12 +108,12 @@ int main() { puts("AND 1, 1 no good"); ae2f_float_t tmp[3] = {}; - err2 = ae2f_AnnSlpPredict( + err2 = ae2f_mAnnSlpPredict( ae2f_mAnnMlpLayerV(&Mlp->Mlp, 0), ins, tmp ); - err2 = ae2f_AnnSlpPredict( + err2 = ae2f_mAnnSlpPredict( ae2f_mAnnMlpLayerV(&Mlp->Mlp, 1), tmp, outbuff ); @@ -122,7 +122,7 @@ int main() { err2 = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnSlpPredict( + err2 = ae2f_mAnnSlpPredict( &Mlp->Slp, ins + 6, outbuff ); if(err2) { err2 = err2; goto __failure; @@ -130,7 +130,7 @@ int main() { if(outbuff[0] > 0.5) { printf("AND 0, 0 no good\n"); - err2 = ae2f_AnnSlpPredict( + err2 = ae2f_mAnnSlpPredict( ae2f_mAnnMlpLayerV(&Mlp->Mlp, 0), ins, outbuff ); @@ -139,14 +139,14 @@ int main() { err2 = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnSlpPredict( + err2 = ae2f_mAnnSlpPredict( &Mlp->Slp, ins + 4, outbuff ); if(err2) { err_ae2f = err2; goto __failure; } printf("Checking the value: %f\n", outbuff[0]); if(outbuff[0] > 0.5) { printf("AND 0, 1 no good\n"); - err2 = ae2f_AnnSlpPredict( + err2 = ae2f_mAnnSlpPredict( ae2f_mAnnMlpLayerV(&Mlp->Mlp, 0), ins, outbuff ); @@ -154,14 +154,14 @@ int main() { err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnSlpPredict( + err2 = ae2f_mAnnSlpPredict( &Mlp->Mlp, ins + 2, outbuff ); if(err2) { err_ae2f = err2; goto __failure; } printf("Checking the value: %f\n", outbuff[0]); if(outbuff[0] > 0.5) { printf("AND 1, 0 no good\n"); - err2 = ae2f_AnnSlpPredict( + err2 = ae2f_mAnnSlpPredict( ae2f_mAnnMlpLayerV(&Mlp->Mlp, 0), ins, outbuff ); @@ -172,7 +172,7 @@ int main() { #endif __failure: - ae2f_AnnSlpDel(&Mlp->Slp); + ae2f_mAnnSlpDel(&Mlp->Slp); printf("Something is over, code: %d\n", err_ae2f); return err_ae2f | err2; } \ No newline at end of file diff --git a/test/src/MlpTrainXOR.cc b/test/src/MlpTrainXOR.cc index 2598cb5..b5120eb 100644 --- a/test/src/MlpTrainXOR.cc +++ b/test/src/MlpTrainXOR.cc @@ -63,7 +63,7 @@ int mainc() { goto __failure; } for(size_t _ = 0; _ < gEpochs; _++) { - err2 = ae2f_AnnSlpTrainB( + err2 = ae2f_mAnnSlpTrainB( &Mlp->Slp, ins, goals + 2, gLearningRate ); @@ -71,7 +71,7 @@ int mainc() { err_ae2f = err2; goto __failure; } - err2 = ae2f_AnnSlpTrainB( + err2 = ae2f_mAnnSlpTrainB( &Mlp->Slp, ins + 2, goals, gLearningRate ); @@ -79,7 +79,7 @@ int mainc() { goto __failure; } - err2 = ae2f_AnnSlpTrainB( + err2 = ae2f_mAnnSlpTrainB( &Mlp->Slp, ins + 4, goals, gLearningRate ); @@ -87,7 +87,7 @@ int mainc() { goto __failure; } - err2 = ae2f_AnnSlpTrainB( + err2 = ae2f_mAnnSlpTrainB( &Mlp->Slp, ins + 6, goals + 2, gLearningRate ); @@ -100,7 +100,7 @@ int mainc() { #if 1 puts("Predict time"); - err2 = ae2f_AnnSlpPredict( + err2 = ae2f_mAnnSlpPredict( &Mlp->Slp, ins, outbuff ); if(err2) { goto __failure; @@ -110,7 +110,7 @@ int mainc() { err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnSlpPredict( + err2 = ae2f_mAnnSlpPredict( &Mlp->Slp, ins + 6, outbuff ); if(err2) { err2 = err2; goto __failure; @@ -120,7 +120,7 @@ int mainc() { err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnSlpPredict( + err2 = ae2f_mAnnSlpPredict( &Mlp->Slp, ins + 4, outbuff ); if(err2) { err_ae2f = err2; goto __failure; @@ -130,7 +130,7 @@ int mainc() { err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnSlpPredict( + err2 = ae2f_mAnnSlpPredict( &Mlp->Slp, ins + 2, outbuff ); if(err2) { err_ae2f = err2; goto __failure; @@ -143,7 +143,7 @@ int mainc() { #endif __failure: - ae2f_AnnSlpDel(&Mlp->Slp); + ae2f_mAnnSlpDel(&Mlp->Slp); printf("Something is over, code: %d\n", err_ae2f | err2); return err_ae2f | err2; } @@ -211,7 +211,7 @@ int maincc() { #if 1 puts("Predict time"); - err2 = ae2f_AnnSlpPredict( + err2 = ae2f_mAnnSlpPredict( &Mlp->Slp, ins, outbuff ); if(err2) { goto __failure; @@ -221,7 +221,7 @@ int maincc() { err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnSlpPredict( + err2 = ae2f_mAnnSlpPredict( &Mlp->Slp, ins + 6, outbuff ); if(err2) { err2 = err2; goto __failure; @@ -231,7 +231,7 @@ int maincc() { err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnSlpPredict( + err2 = ae2f_mAnnSlpPredict( &Mlp->Slp, ins + 4, outbuff ); if(err2) { err_ae2f = err2; goto __failure; @@ -241,7 +241,7 @@ int maincc() { err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnSlpPredict( + err2 = ae2f_mAnnSlpPredict( &Mlp->Slp, ins + 2, outbuff ); if(err2) { err_ae2f = err2; goto __failure; diff --git a/test/src/PercMk.cc b/test/src/PercMk.cc index e985ecd..43b2a2a 100644 --- a/test/src/PercMk.cc +++ b/test/src/PercMk.cc @@ -24,7 +24,7 @@ ae2f_extern int mainc() { ae2f_mAnnSp* Perc; ae2f_float_t out_checksum = 0; - Perc = (ae2f_mAnnSp*)ae2f_mAnnSpMk( + Perc = (ae2f_mAnnSp*)ae2f_AnnSpMk( sizeof(Buff)/sizeof(ae2f_float_t), Buff, Sigmoid, Sub, &err, 0 ); @@ -71,7 +71,7 @@ int maincc() { ae2f_AnnSp* Perc; ae2f_float_t out_checksum = 0; - Perc = ae2f_mAnnSpMk( + Perc = ae2f_AnnSpMk( sizeof(Buff)/sizeof(ae2f_float_t), Buff, Sigmoid, Sub, &err, 0 ); diff --git a/test/src/SlpTrainAND.cc b/test/src/SlpTrainAND.cc index b321549..0956806 100644 --- a/test/src/SlpTrainAND.cc +++ b/test/src/SlpTrainAND.cc @@ -47,7 +47,7 @@ int mainc() { ae2f_float_t outbuff[2] = { 5 }; #define ae2fCL_AnnSlpPredict(obj, inb, _, in_idx, __, out, ...) \ - ae2f_AnnSlpPredict(obj, inb + in_idx, out) + ae2f_mAnnSlpPredict(&obj->Slp, inb + in_idx, out) err2 = ae2fCL_AnnSlpPredict( Slp, ins, 0, @@ -84,36 +84,37 @@ int mainc() { if(err2) { err = err2; goto __failure; } - #define ae2fCL_AnnSlpTrain(obj, ins, _, in_idx, __, goal, learnrate, ...) ae2f_AnnSlpTrainB(obj, ins + in_idx, goal, learnrate) + + Slp->Slp.vPredict; if(err) goto __failure; for(size_t _ = 0; _ < gEpochs; _++) { - err2 = ae2f_AnnSlpTrainB( - Slp, ins, + err2 = ae2f_mAnnSlpTrainB( + &Slp->Slp, ins, goals + 0, gLearningRate ); if(err2) { err = err2; goto __failure; } - err2 = ae2f_AnnSlpTrainB( - Slp, ins + 2/*in_idx*/, + err2 = ae2f_mAnnSlpTrainB( + &Slp->Slp, ins + 2/*in_idx*/, goals + 2, gLearningRate ); if(err2) { err = err2; goto __failure; } - err2 = ae2f_AnnSlpTrainB( - Slp, ins + 4/*in_idx*/, + err2 = ae2f_mAnnSlpTrainB( + &Slp->Slp, ins + 4/*in_idx*/, goals + 2, gLearningRate ); if(err2) { err = err2; goto __failure; } - err2 = ae2f_AnnSlpTrainB( - Slp, ins + 6/*in_idx*/, + err2 = ae2f_mAnnSlpTrainB( + &Slp->Slp, ins + 6/*in_idx*/, goals + 2, gLearningRate ); if(err2) { @@ -143,7 +144,7 @@ int mainc() { err = err2; goto __failure; } printf("Checking the value: %f\n", outbuff[0]); if(outbuff[0] > 0.5) { - err2 = ae2f_mAnnSpPredict(ae2f_AnnSlpPerV(Slp, 0), ins + 6, outbuff); + err2 = ae2f_mAnnSpPredict(ae2f_mAnnSlpPerV(&Slp->Slp, 0), ins + 6, outbuff); printf("Checking the value (SP): %f\n", outbuff[0]); printf("AND 0, 0 no good\n"); @@ -204,7 +205,7 @@ int maincc() { #undef ae2fCL_AnnSlpPredict #define ae2fCL_AnnSlpPredict(obj, inb, _, in_idx, __, out, ...) \ - (obj)->Predict(inb + in_idx, out) // ae2f_AnnSlpPredict(obj, inb + in_idx, out) + (obj)->Slp.Predict(inb + in_idx, out) // ae2f_mAnnSlpPredict(obj, inb + in_idx, out) err2 = ae2fCL_AnnSlpPredict( Slp, ins, 0, @@ -244,36 +245,35 @@ int maincc() { #undef ae2fCL_AnnSlpTrain #define ae2fCL_AnnSlpTrain(obj, ins, _, in_idx, __, goal, learnrate, ...) \ - (obs)->TrainB(ins + in_idx, goal, learnrate) // ae2f_AnnSlpTrainB(obj, ins + in_idx, goal, learnrate) + (obs)->Slp.TrainB(ins + in_idx, goal, learnrate) // ae2f_mAnnSlpTrainB(obj, ins + in_idx, goal, learnrate) if(err) goto __failure; for(size_t _ = 0; _ < gEpochs; _++) { - err2 = ae2f_AnnSlpTrainB( - Slp, ins, - goals + 0, gLearningRate + err2 = Slp->Slp.TrainB( + ins, goals + 0, gLearningRate ); if(err2) { err = err2; goto __failure; } - err2 = ae2f_AnnSlpTrainB( - Slp, ins + 2/*in_idx*/, + err2 = Slp->Slp.TrainB( + ins + 2/*in_idx*/, goals + 2, gLearningRate ); if(err2) { err = err2; goto __failure; } - err2 = ae2f_AnnSlpTrainB( - Slp, ins + 4/*in_idx*/, + err2 = Slp->Slp.TrainB( + ins + 4/*in_idx*/, goals + 2, gLearningRate ); if(err2) { err = err2; goto __failure; } - err2 = ae2f_AnnSlpTrainB( - Slp, ins + 6/*in_idx*/, + err2 = Slp->Slp.TrainB( + ins + 6/*in_idx*/, goals + 2, gLearningRate ); if(err2) { @@ -303,7 +303,7 @@ int maincc() { err = err2; goto __failure; } printf("Checking the value: %f\n", outbuff[0]); if(outbuff[0] > 0.5) { - err2 = ae2f_mAnnSpPredict(ae2f_AnnSlpPerV(Slp, 0), ins + 6, outbuff); + err2 = Slp->Slp.Perc(0)->Predict(ins + 6, outbuff); printf("Checking the value (SP): %f\n", outbuff[0]); printf("AND 0, 0 no good\n"); @@ -338,4 +338,4 @@ int maincc() { ae2f_AnnSlpDel(Slp); printf("ERR: %d\n", err); return err; -} \ No newline at end of file +} diff --git a/test/src/SpTrainAND.c b/test/src/SpTrainAND.c index 5a5ae08..6fe4b05 100644 --- a/test/src/SpTrainAND.c +++ b/test/src/SpTrainAND.c @@ -45,7 +45,7 @@ int main() { ae2f_float_t diff_got[2]; ae2f_err_t err2, err; - ae2f_mAnnSp* Slp = (ae2f_mAnnSp*)ae2f_mAnnSpMk( + ae2f_mAnnSp* Slp = (ae2f_mAnnSp*)ae2f_AnnSpMk( 2, 0, Forward, Backward, &err2, 0 ); From ac35184396e4b35a9722adad641af38965dc0b34 Mon Sep 17 00:00:00 2001 From: ae2f Date: Thu, 13 Mar 2025 07:27:19 +0000 Subject: [PATCH 04/13] Memory leak from Mlp sanitised. --- src/ann-h/Mlp.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ann-h/Mlp.h b/src/ann-h/Mlp.h index 86f5842..5635682 100644 --- a/src/ann-h/Mlp.h +++ b/src/ann-h/Mlp.h @@ -140,7 +140,7 @@ static ae2f_err_t Clean( ) noexcept { if(!_this) return ae2f_errGlob_PTR_IS_NULL; - for(size_t i = 0; i < _this->layerc - 1; i++) { + for(size_t i = 0; i < _this->layerc; i++) { union { size_t** unused; @@ -155,7 +155,8 @@ static ae2f_err_t Clean( ae2f_mAnnMlpLayerVPad(_this, )[i] = 0; } - if(*ae2f_mAnnMlpCache(_this)) free(*ae2f_mAnnMlpCache(_this)); + if(*ae2f_mAnnMlpCache(_this)) + free(*ae2f_mAnnMlpCache(_this)); _this->inc = 0; _this->outc = 0; From b704a65c6124121e7806ce33518e062094346848 Mon Sep 17 00:00:00 2001 From: ae2f Date: Thu, 13 Mar 2025 08:23:11 +0000 Subject: [PATCH 05/13] Refactor + fixed definate memory lost CL-Ann-Test-PercMk.cc --- CMakeLists.txt | 4 +++ include/ae2f/Ann/Mlp.h | 3 --- include/ae2f/Ann/Mlp.h.cxx/mMlp.hh | 5 +++- include/ae2fCL/Ann/Sp.h | 42 +++++++++++++++++------------- include/ae2fCL/Ann/Sp.h.cxx/Sp.hh | 14 ++++------ include/ae2fCL/Ann/Sp.h.cxx/imp.hh | 24 ++++++++++++----- include/ae2fCL/Ann/Sp.h.cxx/mSp.hh | 20 ++++++++++++++ src/cl/Sp.c | 26 +++++++++--------- test/cl-src/PercMk.cc | 29 ++++++++++++--------- test/cl-src/SpTrainAND.c | 20 +++++++------- 10 files changed, 113 insertions(+), 74 deletions(-) create mode 100644 include/ae2fCL/Ann/Sp.h.cxx/mSp.hh diff --git a/CMakeLists.txt b/CMakeLists.txt index aac31f3..32d28c6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,14 +19,17 @@ if(NOT TARGET ${PROJECT_NAME}) "include/ae2f/Ann/Sp.h.c/clean.script.h" "include/ae2f/Ann/Sp.h.c/cleandef.h" "include/ae2f/Ann/Sp.h.cxx/Sp.hh" + "include/ae2f/Ann/Sp.h.cxx/mSp.hh" "include/ae2f/Ann/Sp.h.cxx/imp.hh" "include/ae2f/Ann/Slp.h" "include/ae2f/Ann/Slp.h.cxx/Slp.hh" + "include/ae2f/Ann/Slp.h.cxx/mSlp.hh" "include/ae2f/Ann/Slp.h.cxx/imp.hh" "include/ae2f/Ann/Mlp.h" "include/ae2f/Ann/Mlp.h.cxx/Mlp.hh" + "include/ae2f/Ann/Mlp.h.cxx/mMlp.hh" "include/ae2f/Ann/Mlp.h.cxx/imp.hh" ) target_link_libraries(Ann-H INTERFACE Core) @@ -78,6 +81,7 @@ if(ae2fCL_needed) ${PROJECT_SOURCE_DIR}/Config/CL-Ann-H.cmake.in "include/ae2fCL/Ann.h" "include/ae2fCL/Ann/Sp.h.cxx/Sp.hh" + "include/ae2fCL/Ann/Sp.h.cxx/mSp.hh" "include/ae2fCL/Ann/Sp.h.cxx/imp.hh" "include/ae2fCL/Ann/Sp.h" ) diff --git a/include/ae2f/Ann/Mlp.h b/include/ae2f/Ann/Mlp.h index 0510a92..62aaf9d 100644 --- a/include/ae2f/Ann/Mlp.h +++ b/include/ae2f/Ann/Mlp.h @@ -23,9 +23,6 @@ typedef ae2f_mAnnSlp ae2f_mAnnMlpEl; struct ae2f_mAnnMlp { #include "Mlp.h.cxx/mMlp.hh" - -private: -ae2f_mAnnSlp _this[1]; }; #else diff --git a/include/ae2f/Ann/Mlp.h.cxx/mMlp.hh b/include/ae2f/Ann/Mlp.h.cxx/mMlp.hh index ef0fcf4..f3340ba 100644 --- a/include/ae2f/Ann/Mlp.h.cxx/mMlp.hh +++ b/include/ae2f/Ann/Mlp.h.cxx/mMlp.hh @@ -19,4 +19,7 @@ constexprfun const ae2f_mAnnMlpEl* ae2f_TMP Perc(size_t a) const; constexprfun size_t* ae2f_TMP PercPad(size_t); constexprfun const size_t* ae2f_TMP PercPad(size_t) const; -#undef ae2f_TMP \ No newline at end of file +#undef ae2f_TMP + +private: +ae2f_mAnnSlp _this[1]; \ No newline at end of file diff --git a/include/ae2fCL/Ann/Sp.h b/include/ae2fCL/Ann/Sp.h index d3886a9..2ed68e7 100644 --- a/include/ae2fCL/Ann/Sp.h +++ b/include/ae2fCL/Ann/Sp.h @@ -22,35 +22,29 @@ #include #if ae2f_WhenCXX(!) 0 -struct ae2fCL_AnnSp : ae2f_mAnnSp { - #include "./Sp.h.cxx/Sp.hh" +struct ae2fCL_mAnnSp { +#include "./Sp.h.cxx/mSp.hh" }; #else -/// @class ae2fCL_AnnSp +/// @class ae2fCL_mAnnSp /// @extends ae2f_mAnnSp /// @brief /// Hello World -typedef ae2f_mAnnSp ae2fCL_AnnSp; +typedef ae2f_mAnnSp ae2fCL_mAnnSp; #endif -#define ae2fCL_AnnSpPredict ae2f_mAnnSpPredict -#define ae2fCL_AnnSpTrain ae2f_mAnnSpTrain -#define ae2fCL_AnnSpTrainA ae2f_mAnnSpTrainA -#define ae2fCL_AnnSpTrainB ae2f_mAnnSpTrainB -#define ae2fCL_AnnSpClean ae2f_mAnnSpClean -#define ae2fCL_AnnSpB ae2f_mAnnSpB -#define ae2fCL_AnnSpW ae2f_mAnnSpW -#define ae2fCL_AnnSpDel ae2f_mAnnSpDel +#define ae2fCL_mAnnSpClean ae2f_mAnnSpClean +#define ae2fCL_mAnnSpDel ae2f_mAnnSpDel -#define ae2fCL_AnnSpInitSz(off, inc) ae2f_mAnnSpInitSz((off) + (cl_mem_SIZE << 2), inc) -#define ae2fCL_AnnSpWCl(per, ...) ae2f_mAnnSpX(per, cl_mem*, __VA_ARGS__) -#define ae2fCL_AnnSpIOCl(per, ...) (ae2fCL_AnnSpWCl(per, __VA_ARGS__) + 1) -#define ae2fCL_AnnSpX(per, type, ...) ae2f_reinterpret_cast(__VA_ARGS__ type, ae2fCL_AnnSpIOCl(per, __VA_ARGS__) + 1) +#define ae2fCL_mAnnSpInitSz(off, inc) ae2f_mAnnSpInitSz((off) + (cl_mem_SIZE << 2), inc) +#define ae2fCL_mAnnSpWCl(per, ...) ae2f_mAnnSpX(per, cl_mem*, __VA_ARGS__) +#define ae2fCL_mAnnSpIOCl(per, ...) (ae2fCL_mAnnSpWCl(per, __VA_ARGS__) + 1) +#define ae2fCL_mAnnSpX(per, type, ...) ae2f_reinterpret_cast(__VA_ARGS__ type, ae2fCL_mAnnSpIOCl(per, __VA_ARGS__) + 1) ae2f_extern ae2f_SHAREDCALL -size_t ae2fCL_AnnSpInit( - ae2fCL_AnnSp* perc_opt, +size_t ae2fCL_mAnnSpInit( + ae2fCL_mAnnSp* perc_opt, size_t icount, const ae2f_float_t* w_opt, ae2f_AnnAct_t Act, @@ -60,6 +54,18 @@ size_t ae2fCL_AnnSpInit( size_t off_opt ) noexcept; +typedef union ae2fCL_AnnSp { + ae2fCL_mAnnSp CL_Sp; + ae2f_mAnnSp Sp; + + #if ae2f_WhenCXX(!)0 + #include "Sp.h.cxx/Sp.hh" + #endif +} ae2fCL_AnnSp; + +#define ae2fCL_AnnSpClean ae2f_AnnSpClean +#define ae2fCL_AnnSpDel ae2f_AnnSpDel + ae2f_extern ae2f_SHAREDCALL ae2fCL_AnnSp* ae2fCL_AnnSpMk( size_t icount, diff --git a/include/ae2fCL/Ann/Sp.h.cxx/Sp.hh b/include/ae2fCL/Ann/Sp.h.cxx/Sp.hh index d78684f..6a892a5 100644 --- a/include/ae2fCL/Ann/Sp.h.cxx/Sp.hh +++ b/include/ae2fCL/Ann/Sp.h.cxx/Sp.hh @@ -2,16 +2,12 @@ #define ae2f_TMP -constexprfun cl_mem* ae2f_TMP WCl(); -constexprfun const cl_mem* ae2f_TMP WCl() const; +inline ae2f_TMP ~ae2fCL_AnnSp(); -constexprfun cl_mem* ae2f_TMP IOCl(); -constexprfun const cl_mem* ae2f_TMP IOCl() const; +private: + +uint8_t _; +constexprfun ae2fCL_AnnSp() : _(0) {} -template -constexprfun const T* ae2f_TMP X() const; - -template -constexprfun T* ae2f_TMP X(); #undef ae2f_TMP \ No newline at end of file diff --git a/include/ae2fCL/Ann/Sp.h.cxx/imp.hh b/include/ae2fCL/Ann/Sp.h.cxx/imp.hh index 2eeba58..d82515c 100644 --- a/include/ae2fCL/Ann/Sp.h.cxx/imp.hh +++ b/include/ae2fCL/Ann/Sp.h.cxx/imp.hh @@ -2,30 +2,40 @@ -#define ae2f_TMP ae2fCL_AnnSp:: +#define ae2f_TMP ae2fCL_mAnnSp:: constexprfun cl_mem* ae2f_TMP WCl() { - return ae2fCL_AnnSpWCl(this); + return ae2fCL_mAnnSpWCl(_this); } constexprfun const cl_mem* ae2f_TMP WCl() const { - return ae2fCL_AnnSpWCl(this, const); + return ae2fCL_mAnnSpWCl(_this, const); } constexprfun cl_mem* ae2f_TMP IOCl() { - return ae2fCL_AnnSpIOCl(this); + return ae2fCL_mAnnSpIOCl(_this); } constexprfun const cl_mem* ae2f_TMP IOCl() const { - return ae2fCL_AnnSpIOCl(this, const); + return ae2fCL_mAnnSpIOCl(_this, const); } template constexprfun const T* ae2f_TMP X() const { - return ae2fCL_AnnSpX(this, T*, const); + return ae2fCL_mAnnSpX(_this, T*, const); } template constexprfun T* ae2f_TMP X() { - return ae2fCL_AnnSpX(this, T*); + return ae2fCL_mAnnSpX(_this, T*); +} + +#undef ae2f_TMP + + +#define ae2f_TMP ae2fCL_AnnSp:: + +inline ae2f_TMP ~ae2fCL_AnnSp() { + ae2fCL_AnnSpClean(this); + return; } #undef ae2f_TMP \ No newline at end of file diff --git a/include/ae2fCL/Ann/Sp.h.cxx/mSp.hh b/include/ae2fCL/Ann/Sp.h.cxx/mSp.hh new file mode 100644 index 0000000..102cdfd --- /dev/null +++ b/include/ae2fCL/Ann/Sp.h.cxx/mSp.hh @@ -0,0 +1,20 @@ +#include "../Sp.h" + +#define ae2f_TMP + +constexprfun cl_mem* ae2f_TMP WCl(); +constexprfun const cl_mem* ae2f_TMP WCl() const; + +constexprfun cl_mem* ae2f_TMP IOCl(); +constexprfun const cl_mem* ae2f_TMP IOCl() const; + +template +constexprfun const T* ae2f_TMP X() const; + +template +constexprfun T* ae2f_TMP X(); + +#undef ae2f_TMP + +private: +ae2f_mAnnSp _this[1]; \ No newline at end of file diff --git a/src/cl/Sp.c b/src/cl/Sp.c index f69c60b..0e57f1a 100644 --- a/src/cl/Sp.c +++ b/src/cl/Sp.c @@ -16,7 +16,7 @@ static ae2f_err_t Predict( if(!in) return ae2f_errGlob_PTR_IS_NULL; if(!outret_opt) return ae2f_errGlob_PTR_IS_NULL | ae2f_errGlob_DONE_HOWEV; - cl_mem _W = *ae2fCL_AnnSpWCl(_this, const), _IO = *ae2fCL_AnnSpIOCl(_this, const); + cl_mem _W = *ae2fCL_mAnnSpWCl(_this, const), _IO = *ae2fCL_mAnnSpIOCl(_this, const); cl_kernel K = ae2fCL_Ann.Kerns[ae2fCL_eAnnKernsSpPredict]; ae2f_float_t IBuffer; @@ -45,7 +45,7 @@ static ae2f_err_t Predict( ae2fCL_Ann.LErr = clEnqueueReadBuffer(ae2fCL_Ann.Q, _IO, CL_TRUE, _this->inc * ae2f_float_t_SIZE, ae2f_float_t_SIZE, &IBuffer, 0, 0, 0); if(ae2fCL_Ann.LErr != CL_SUCCESS) return ae2f_errGlob_NFOUND; - IBuffer = _this->Act(IBuffer + *ae2fCL_AnnSpB(_this, const)); + IBuffer = _this->Act(IBuffer + *ae2f_mAnnSpB(_this, const)); if(outret_opt) *outret_opt = IBuffer; return ae2f_errGlob_OK; @@ -77,7 +77,7 @@ static ae2f_err_t Train( *uf.F *= learningrate; *ae2f_mAnnSpB(_this) += *uf.F; - cl_mem _W = *ae2fCL_AnnSpWCl(_this), _IO = *ae2fCL_AnnSpIOCl(_this); + cl_mem _W = *ae2fCL_mAnnSpWCl(_this), _IO = *ae2fCL_mAnnSpIOCl(_this); cl_kernel K = ae2fCL_Ann.Kerns[ae2fCL_eAnnKernsSpTrain]; ae2fCL_Ann.LErr = clSetKernelArg(K, 0, sizeof(cl_mem), &_IO); if(ae2fCL_Ann.LErr != CL_SUCCESS) return ae2f_errGlob_NFOUND; @@ -114,8 +114,8 @@ static ae2f_err_t Train( } ae2f_SHAREDEXPORT -size_t ae2fCL_AnnSpInit( - ae2fCL_AnnSp* perc_opt, +size_t ae2fCL_mAnnSpInit( + ae2fCL_mAnnSp* perc_opt, size_t icount, const ae2f_float_t* w_opt, ae2f_AnnAct_t Act, @@ -131,7 +131,7 @@ size_t ae2fCL_AnnSpInit( ae2f_mAnnSpInit(perc_opt, icount, w_opt, Act, CalDelta, &err, off_opt); if(err) goto END; - *ae2fCL_AnnSpWCl(perc_opt) = clCreateBuffer( + *ae2fCL_mAnnSpWCl(perc_opt) = clCreateBuffer( ae2fCL_Ann.Ctx, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, ae2f_float_t_SIZE * icount, ae2f_mAnnSpW(perc_opt), @@ -140,7 +140,7 @@ size_t ae2fCL_AnnSpInit( if(err2 != CL_SUCCESS) return(ae2f_errGlob_NFOUND | ae2f_errGlob_ALLOC_FAILED); - *ae2fCL_AnnSpIOCl(perc_opt) = clCreateBuffer( + *ae2fCL_mAnnSpIOCl(perc_opt) = clCreateBuffer( ae2fCL_Ann.Ctx, CL_MEM_READ_WRITE, ae2f_float_t_SIZE * (icount + 1), 0, &err2 @@ -156,7 +156,7 @@ size_t ae2fCL_AnnSpInit( #undef return if(errret_opt) *errret_opt = err; if(erronnfound_opt) *erronnfound_opt = err2; - return ae2fCL_AnnSpInitSz(off_opt, icount); + return ae2fCL_mAnnSpInitSz(off_opt, icount); } ae2f_SHAREDEXPORT @@ -169,17 +169,17 @@ ae2fCL_AnnSp* ae2fCL_AnnSpMk( cl_int* erronnfound_opt, size_t off_opt ) noexcept { - ae2f_mAnnSp* v; - v = calloc(ae2fCL_AnnSpInitSz(off_opt, icount), 1); - ae2fCL_AnnSpInit(v, icount, w_opt, Act, CalDelta, errret_opt, erronnfound_opt, off_opt); + ae2fCL_AnnSp* v; + v = calloc(ae2fCL_mAnnSpInitSz(off_opt, icount), 1); + ae2fCL_mAnnSpInit(&v->CL_Sp, icount, w_opt, Act, CalDelta, errret_opt, erronnfound_opt, off_opt); return v; } static ae2f_err_t Clean(ae2f_mAnnSp* a) { if(!a) return ae2f_errGlob_PTR_IS_NULL; cl_int er[2]; - er[0] = clReleaseMemObject(*ae2fCL_AnnSpIOCl(a)); - er[1] = clReleaseMemObject(*ae2fCL_AnnSpWCl(a)); + er[0] = clReleaseMemObject(*ae2fCL_mAnnSpIOCl(a)); + er[1] = clReleaseMemObject(*ae2fCL_mAnnSpWCl(a)); if(er[0] || er[1]) return ae2f_errGlob_NFOUND; diff --git a/test/cl-src/PercMk.cc b/test/cl-src/PercMk.cc index 36baca4..1861ff1 100644 --- a/test/cl-src/PercMk.cc +++ b/test/cl-src/PercMk.cc @@ -34,24 +34,27 @@ int mainc() { Sigmoid, Sub, &err, errcl, 0 ); CHECK_ERR(err, CL_SUCCESS, __failure); - err = Perc->Predict(Buff, &outfloat); + + err = ae2f_mAnnSpPredict( + &Perc->Sp, Buff, &outfloat + ); if(err) goto __failure; printf("out: %f\n", outfloat); printf( "Bias global: %f, with bias: %f\n", - *ae2f_mAnnSpB(Perc), - (*ae2f_mAnnSpB(Perc)) * sizeof(Buff)/ sizeof(ae2f_float_t) + outfloat + *ae2f_mAnnSpB(&Perc->Sp), + (*ae2f_mAnnSpB(&Perc->Sp)) * sizeof(Buff)/ sizeof(ae2f_float_t) + outfloat ); - for(size_t i = 0; i < Perc->inc; i++) + for(size_t i = 0; i < Perc->Sp.inc; i++) { ae2f_float_t got = Buff[i] * Buff[i]; out_checksum += got; printf("Check-got: %f\n", got); } - out_checksum += *Perc->B(); - out_checksum = Perc->Act(out_checksum); + out_checksum += *ae2f_mAnnSpB(&Perc->Sp); + out_checksum = Perc->Sp.Act(out_checksum); printf("Checking two values match...: %f %f\n", out_checksum, outfloat); if((out_checksum - outfloat) * (out_checksum - outfloat) > gThreshold) { printf("Check failed\n"); @@ -86,25 +89,25 @@ int maincc() { ); CHECK_ERR(err, CL_SUCCESS, __failure); - err = Perc->Predict( + err = Perc->Sp.Predict( Buff, &outfloat ); if(err) goto __failure; printf("out: %f\n", outfloat); printf( "Bias global: %f, with bias: %f\n", - *Perc->B(), - (*Perc->B()) * sizeof(Buff)/ sizeof(ae2f_float_t) + outfloat + *Perc->Sp.B(), + (*Perc->Sp.B()) * sizeof(Buff)/ sizeof(ae2f_float_t) + outfloat ); - for(size_t i = 0; i < Perc->inc; i++) + for(size_t i = 0; i < Perc->Sp.inc; i++) { ae2f_float_t got = Buff[i] * Buff[i]; out_checksum += got; printf("Check-got: %f\n", got); } - out_checksum += *ae2f_mAnnSpB(Perc); - out_checksum = Perc->Act(out_checksum); + out_checksum += *Perc->Sp.B(); + out_checksum = Perc->Sp.Act(out_checksum); printf("Checking two values match...: %f %f\n", out_checksum, outfloat); if((out_checksum - outfloat) * (out_checksum - outfloat) > gThreshold) { printf("Check failed\n"); @@ -113,10 +116,10 @@ int maincc() { __failure: + if(Perc) delete (Perc); ae2fCL_AnnDel(); if(ae2fCL_Ann.Q) clReleaseCommandQueue(ae2fCL_Ann.Q); if(ae2fCL_Ann.Ctx) clReleaseContext(ae2fCL_Ann.Ctx); - if(Perc) delete (Perc); return err; } diff --git a/test/cl-src/SpTrainAND.c b/test/cl-src/SpTrainAND.c index ff578f3..31b96af 100644 --- a/test/cl-src/SpTrainAND.c +++ b/test/cl-src/SpTrainAND.c @@ -48,7 +48,7 @@ int main() { ae2fCL_AnnMkEasy(0); ae2f_err_t err2, err; - ae2f_mAnnSp* Slp = ae2fCL_AnnSpMk( + ae2fCL_AnnSp* Perc = ae2fCL_AnnSpMk( 2, 0, Forward, Backward, &err2, 0, 0 ); @@ -60,7 +60,7 @@ int main() { if(err) goto __failure; for(size_t _ = 0; _ < gEpochs; _++) { err2 = ae2f_mAnnSpTrainB( - Slp, ins, + &Perc->Sp, ins, *goals + 0, gLearningRate ); if(err2) { @@ -68,7 +68,7 @@ int main() { } err2 = ae2fCL_AnnSlpTrain( - Slp, ins, + &Perc->Sp, ins, 0, 2/*in_idx*/, 0, goals + 2, gLearningRate, 0, (void*)diff_got, 0, @@ -79,7 +79,7 @@ int main() { } err2 = ae2fCL_AnnSlpTrain( - Slp, ins, + &Perc->Sp, ins, 0, 4/*in_idx*/, 0, goals + 2, gLearningRate, 0, (void*)diff_got, 0, @@ -90,7 +90,7 @@ int main() { } err2 = ae2fCL_AnnSlpTrain( - Slp, ins, + &Perc->Sp, ins, 0, 6/*in_idx*/, 0, goals + 2, gLearningRate, 0, diff_got, 0, @@ -106,7 +106,7 @@ int main() { ae2f_mAnnSpPredict(obj, inb + in_idx, (out)) err2 = ae2fCL_AnnSlpPredict( - Slp, ins, 0, + &Perc->Sp, ins, 0, 0/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context ); if(err2) { @@ -118,7 +118,7 @@ int main() { } err2 = ae2fCL_AnnSlpPredict( - Slp, ins, 0, + &Perc->Sp, ins, 0, 6/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context ); if(err2) { @@ -130,7 +130,7 @@ int main() { } err2 = ae2fCL_AnnSlpPredict( - Slp, ins, 0, + &Perc->Sp, ins, 0, 4/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context ); if(err2) { @@ -142,7 +142,7 @@ int main() { } err2 = ae2fCL_AnnSlpPredict( - Slp, ins, 0, + &Perc->Sp, ins, 0, 2/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context ); if(err2) { @@ -154,7 +154,7 @@ int main() { } __failure: - ae2f_mAnnSpDel(Slp); + ae2fCL_AnnSpDel(Perc); ae2fCL_AnnDel(); return err | err2; } \ No newline at end of file From d94b6482fce205473052362a9305b1c5d1c77e48 Mon Sep 17 00:00:00 2001 From: ae2f Date: Thu, 13 Mar 2025 09:59:40 +0000 Subject: [PATCH 06/13] Fix: Definate leak of - CL-Ann-Test-SlpTrainAND.c - CL-Ann-Test-MlpTrainXOR.c --- CMakeLists.txt | 6 ++ include/ae2fCL/Ann/Mlp.h | 4 +- include/ae2fCL/Ann/Slp.h | 86 +++++++++++++++++----------- include/ae2fCL/Ann/Slp.h.cxx/imp.hh | 30 ++++++++++ include/ae2fCL/Ann/Slp.h.cxx/mSlp.hh | 20 +++++++ src/cl/Mlp.c | 4 +- src/cl/Slp.c | 26 +++++---- test/cl-src/SlpLeak.c | 19 ++++++ test/cl-src/SlpMk.cc | 16 +++--- test/cl-src/SlpTrainAND.c | 36 ++++++------ test/cl-src/SpTrainAND.c | 18 +++--- test/src/SlpTrainAND.cc | 42 +++++++------- test/src/SpTrainAND.c | 18 +++--- 13 files changed, 208 insertions(+), 117 deletions(-) create mode 100644 include/ae2fCL/Ann/Slp.h.cxx/imp.hh create mode 100644 include/ae2fCL/Ann/Slp.h.cxx/mSlp.hh create mode 100644 test/cl-src/SlpLeak.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 32d28c6..8457071 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -80,10 +80,16 @@ if(ae2fCL_needed) include ae2fCL ${PROJECT_SOURCE_DIR}/Config/CL-Ann-H.cmake.in "include/ae2fCL/Ann.h" + "include/ae2fCL/Ann/Sp.h.cxx/Sp.hh" "include/ae2fCL/Ann/Sp.h.cxx/mSp.hh" "include/ae2fCL/Ann/Sp.h.cxx/imp.hh" "include/ae2fCL/Ann/Sp.h" + + "include/ae2fCL/Ann/Slp.h.cxx/Slp.hh" + "include/ae2fCL/Ann/Slp.h.cxx/mSlp.hh" + "include/ae2fCL/Ann/Slp.h.cxx/imp.hh" + "include/ae2fCL/Ann/Slp.h" ) ae2f_RulerMeasure( diff --git a/include/ae2fCL/Ann/Mlp.h b/include/ae2fCL/Ann/Mlp.h index 70c5863..f96e88f 100644 --- a/include/ae2fCL/Ann/Mlp.h +++ b/include/ae2fCL/Ann/Mlp.h @@ -33,8 +33,6 @@ ae2f_AnnMlp* ae2fCL_AnnMlpMk( cl_int* errnfound_opt ) noexcept; -#define ae2fCL_AnnMlpInitSz ae2f_mAnnMlpInitSz -#define ae2fCL_AnnMlpDel ae2f_mAnnMlpDel -#define ae2fCL_AnnMlpClean ae2f_mAnnMlpClean +#define ae2fCL_mAnnMlpInitSz ae2f_mAnnMlpInitSz #endif \ No newline at end of file diff --git a/include/ae2fCL/Ann/Slp.h b/include/ae2fCL/Ann/Slp.h index 455217a..f99c093 100644 --- a/include/ae2fCL/Ann/Slp.h +++ b/include/ae2fCL/Ann/Slp.h @@ -18,9 +18,31 @@ #include #include +typedef struct ae2fCL_mAnnSlpMemX { + cl_mem In; + + /** @brief Has weights been changed */ + bool Changed; +} ae2fCL_mAnnSlpMemX; + +#if ae2f_WhenCXX(!) 0 +struct ae2fCL_mAnnSlp { +#include "Slp.h.cxx/mSlp.hh" +}; +#else + +/** + * @class ae2fCL_mAnnSlp + * @brief + * SLP with OpenCL acceleration. + */ +typedef ae2f_mAnnSlp ae2fCL_mAnnSlp; + +#endif + ae2f_extern ae2f_SHAREDCALL -size_t ae2fCL_AnnSlpInit( - ae2f_mAnnSlp* _this, +size_t ae2fCL_mAnnSlpInit( + ae2fCL_mAnnSlp* _this, const size_t* incs_optA, size_t ginc_optB, const size_t* inpads_opt, @@ -33,29 +55,23 @@ size_t ae2fCL_AnnSlpInit( cl_int* err_nfound_opt ) noexcept; -#define ae2fCL_AnnSlpInitA(_this, incs, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt, err_nfound_opt) \ -ae2fCL_AnnSlpInit(_this, incs, 0, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt, err_nfound_opt) +#define ae2fCL_mAnnSlpInitA(_this, incs, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt, err_nfound_opt) \ +ae2fCL_mAnnSlpInit(_this, incs, 0, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt, err_nfound_opt) -#define ae2fCL_AnnSlpInitB(_this, ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt, err_nfound_opt) \ -ae2fCL_AnnSlpInit(_this, 0, ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt, err_nfound_opt) +#define ae2fCL_mAnnSlpInitB(_this, ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt, err_nfound_opt) \ +ae2fCL_mAnnSlpInit(_this, 0, ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt, err_nfound_opt) +typedef union ae2fCL_AnnSlp { + ae2f_mAnnSlp Slp; + ae2fCL_mAnnSlp CL_Slp; -#if ae2f_WhenCXX(!) 0 -struct ae2fCL_AnnSlp : ae2f_mAnnSlp { -#include "Slp.h.cxx/Slp.hh" -}; -#else - -/** - * @class ae2fCL_AnnSlp - * @brief - * SLP with OpenCL acceleration. - */ -typedef ae2f_mAnnSlp ae2fCL_AnnSlp; -#endif + #if ae2f_WhenCXX(!)0 + #include "Slp.h.cxx/Slp.hh" + #endif +} ae2fCL_AnnSlp; ae2f_extern ae2f_SHAREDCALL -ae2f_mAnnSlp* ae2fCL_AnnSlpMk( +ae2fCL_AnnSlp* ae2fCL_AnnSlpMk( const size_t* incs_optA, size_t ginc_optB, const size_t* inpads_opt, @@ -68,36 +84,36 @@ ae2f_mAnnSlp* ae2fCL_AnnSlpMk( cl_int* err_nfound_opt ) noexcept; +#define ae2fCL_AnnSlpClean ae2f_AnnSlpClean +#define ae2fCL_AnnSlpDel ae2f_AnnSlpDel + #define ae2fCL_AnnSlpMkA(incs, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt, err_nfound_opt) \ ae2fCL_AnnSlpMk(incs, 0, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt, err_nfound_opt) #define ae2fCL_AnnSlpMkB(ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt, err_nfound_opt) \ ae2fCL_AnnSlpMk(0, ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt, err_nfound_opt) -typedef struct ae2fCL_AnnSlpMemX { - cl_mem In; - - /** @brief Has weights been changed */ - bool Changed; -} ae2fCL_AnnSlpMemX; - /** * @param outc * @param off * */ -#define ae2fCL_AnnSlpInitSz(outc, off) \ - ae2f_mAnnSlpInitSz(outc, (off) + sizeof(ae2fCL_AnnSlpMemX) + (sizeof(ae2f_float_t) * outc)) +#define ae2fCL_mAnnSlpInitSz(outc, off) \ + ae2f_mAnnSlpInitSz(outc, (off) + sizeof(ae2fCL_mAnnSlpMemX) + (sizeof(ae2f_float_t) * outc)) -#define ae2fCL_AnnSlpAdd(slp, ...) \ - ae2f_mAnnSlpX(slp, ae2fCL_AnnSlpMemX*, __VA_ARGS__) +#define ae2fCL_mAnnSlpAdd(slp, ...) \ + ae2f_mAnnSlpX(slp, ae2fCL_mAnnSlpMemX*, __VA_ARGS__) -#define ae2fCL_AnnSlpOutCache(slp, ...) \ - ae2f_reinterpret_cast(__VA_ARGS__ ae2f_float_t*, ae2fCL_AnnSlpAdd(slp, __VA_ARGS__) + 1) +#define ae2fCL_mAnnSlpOutCache(slp, ...) \ + ae2f_reinterpret_cast(__VA_ARGS__ ae2f_float_t*, ae2fCL_mAnnSlpAdd(slp, __VA_ARGS__) + 1) -#define ae2fCL_AnnSlpX(slp, type, ...) \ - ae2f_reinterpret_cast(__VA_ARGS__ type*, ae2fCL_AnnSlpOutCache(slp, __VA_ARGS__) + (slp)->outc) +#define ae2fCL_mAnnSlpX(slp, type, ...) \ + ae2f_reinterpret_cast(__VA_ARGS__ type*, ae2fCL_mAnnSlpOutCache(slp, __VA_ARGS__) + (slp)->outc) #include +#if ae2f_WhenCXX(!)0 +#include "Slp.h.cxx/imp.hh" +#endif + #endif diff --git a/include/ae2fCL/Ann/Slp.h.cxx/imp.hh b/include/ae2fCL/Ann/Slp.h.cxx/imp.hh new file mode 100644 index 0000000..f01f66f --- /dev/null +++ b/include/ae2fCL/Ann/Slp.h.cxx/imp.hh @@ -0,0 +1,30 @@ +#include "../Slp.h" + +#define ae2f_TMP ae2fCL_mAnnSlp:: + +constexprfun const ae2fCL_mAnnSlpMemX* ae2f_TMP Add() const { + return ae2fCL_mAnnSlpAdd(_this, const); +} +constexprfun ae2fCL_mAnnSlpMemX* ae2f_TMP Add() { + return ae2fCL_mAnnSlpAdd(_this); +} + +constexprfun const ae2f_float_t* ae2f_TMP OutCache() const { + return ae2fCL_mAnnSlpOutCache(_this, const); +} + +constexprfun ae2f_float_t* ae2f_TMP OutCache() { + return ae2fCL_mAnnSlpOutCache(_this); +} + +template +constexprfun const T* ae2f_TMP X() const { + return ae2fCL_mAnnSlpX(_this, T, const); +} + +template +constexprfun T* ae2f_TMP X() { + return ae2fCL_mAnnSlpX(_this, T, const); +} + +#undef ae2f_TMP \ No newline at end of file diff --git a/include/ae2fCL/Ann/Slp.h.cxx/mSlp.hh b/include/ae2fCL/Ann/Slp.h.cxx/mSlp.hh new file mode 100644 index 0000000..1da3035 --- /dev/null +++ b/include/ae2fCL/Ann/Slp.h.cxx/mSlp.hh @@ -0,0 +1,20 @@ +#include "../Slp.h" + +#define ae2f_TMP + +constexprfun const ae2fCL_mAnnSlpMemX* ae2f_TMP Add() const; +constexprfun ae2fCL_mAnnSlpMemX* ae2f_TMP Add(); + +constexprfun const ae2f_float_t* ae2f_TMP OutCache() const; +constexprfun ae2f_float_t* ae2f_TMP OutCache(); + +template +constexprfun const T* ae2f_TMP X() const; + +template +constexprfun T* ae2f_TMP X(); + +#undef ae2f_TMP + +private: +ae2f_mAnnSlp _this[1]; \ No newline at end of file diff --git a/src/cl/Mlp.c b/src/cl/Mlp.c index 9d57f01..90d44f5 100644 --- a/src/cl/Mlp.c +++ b/src/cl/Mlp.c @@ -53,9 +53,9 @@ size_t ae2fCL_AnnMlpInit( }* u; } perc = {ae2f_mAnnMlpLayerVPad(_this) + i}; - perc.u->pad = calloc(ae2fCL_AnnSlpInitSz(LAYERSZ_R, sizeof(size_t)), 1); + perc.u->pad = calloc(ae2fCL_mAnnSlpInitSz(LAYERSZ_R, sizeof(size_t)), 1); perc.u->pad++; - ae2fCL_AnnSlpInitB(perc.u->slp, LAYERSZ_L, 0, weights_opt, actglob_opt, deltaglob_opt, LAYERSZ_R, 0, &e, &er_cl); + ae2fCL_mAnnSlpInitB(perc.u->slp, LAYERSZ_L, 0, weights_opt, actglob_opt, deltaglob_opt, LAYERSZ_R, 0, &e, &er_cl); if(er_cl) err2 = er_cl; *(--perc.u->pad) = 0; diff --git a/src/cl/Slp.c b/src/cl/Slp.c index 5e6e661..3767634 100644 --- a/src/cl/Slp.c +++ b/src/cl/Slp.c @@ -39,7 +39,7 @@ ae2f_err_t TrainCL( cl_int err = 0, err2 = 0; - ae2fCL_AnnSlpMemX* memx = ae2fCL_AnnSlpAdd(_this); + ae2fCL_mAnnSlpMemX* memx = ae2fCL_mAnnSlpAdd(_this); const size_t WORKSZ[2] = { _this->inc, _this->outc }, @@ -48,10 +48,12 @@ ae2f_err_t TrainCL( #define IC WORKSZ[0] #define OC WORKSZ[1] - ae2f_float_t* PREDICTED_BUFF = ae2fCL_AnnSlpOutCache(_this); + ae2f_float_t* PREDICTED_BUFF = ae2fCL_mAnnSlpOutCache(_this); + #if 0 if(!(PREDICTED_BUFF = calloc(OC, sizeof(ae2f_float_t)))) return(ae2f_errGlob_ALLOC_FAILED); + #endif if(delta_optA) { @@ -248,7 +250,7 @@ ae2f_err_t PredictCL( cl_int err = 0, err2 = 0; ae2f_err_t code = 0; - ae2fCL_AnnSlpMemX* __X = ae2fCL_AnnSlpAdd(_); + ae2fCL_mAnnSlpMemX* __X = ae2fCL_mAnnSlpAdd(_); const cl_kernel KERNEL = ae2fCL_AnnKerns[ae2fCL_eAnnKernsSlpPredict]; if(!__X->In) return ae2f_errGlob_PTR_IS_NULL | ae2f_errGlob_ALLOC_FAILED; @@ -393,7 +395,7 @@ ae2f_err_t PredictCL( static ae2f_mAnnSlpClean_t CleanCL; ae2f_SHAREDEXPORT -size_t ae2fCL_AnnSlpInit( +size_t ae2fCL_mAnnSlpInit( ae2f_mAnnSlp* _this, const size_t* incs_optA, size_t ginc_optB, @@ -455,7 +457,7 @@ size_t ae2fCL_AnnSlpInit( } } - ae2fCL_AnnSlpAdd(_this)->In = + ae2fCL_mAnnSlpAdd(_this)->In = clCreateBuffer( ae2fCL_Ann.Ctx, CL_MEM_READ_WRITE, @@ -463,7 +465,7 @@ size_t ae2fCL_AnnSlpInit( NULL, &v2 ); - ae2fCL_AnnSlpAdd(_this)->Changed = 1; + ae2fCL_mAnnSlpAdd(_this)->Changed = 1; if(CL_SUCCESS != v2) V = v2; @@ -471,14 +473,14 @@ size_t ae2fCL_AnnSlpInit( DONE: if(err_opt) *err_opt = er; if(errnfound_opt) *errnfound_opt = V; - return ae2fCL_AnnSlpInitSz(outc, offset_opt); + return ae2fCL_mAnnSlpInitSz(outc, offset_opt); } static ae2f_err_t CleanCL(ae2f_mAnnSlp* _) { ae2f_err_t e = 0; - if(_ && ae2fCL_AnnSlpAdd(_)->In) { + if(_ && ae2fCL_mAnnSlpAdd(_)->In) { if((ae2fCL_Ann.LErr = clReleaseMemObject( - ae2fCL_AnnSlpAdd(_)->In + ae2fCL_mAnnSlpAdd(_)->In ))) { e = @@ -491,7 +493,7 @@ static ae2f_err_t CleanCL(ae2f_mAnnSlp* _) { } ae2f_SHAREDEXPORT -ae2f_mAnnSlp* ae2fCL_AnnSlpMk( +ae2fCL_AnnSlp* ae2fCL_AnnSlpMk( const size_t* incs_optA, size_t ginc_optB, const size_t* inpads_opt, @@ -503,11 +505,11 @@ ae2f_mAnnSlp* ae2fCL_AnnSlpMk( ae2f_err_t* err_opt, cl_int* err_nfound_opt ) noexcept { - ae2f_mAnnSlp* rtn = calloc(ae2fCL_AnnSlpInitSz(outc, offset_opt), 1); + ae2fCL_AnnSlp* rtn = calloc(ae2fCL_mAnnSlpInitSz(outc, offset_opt), 1); ae2f_err_t err = 0; cl_int err2 = 0; - ae2fCL_AnnSlpInit(rtn, incs_optA, ginc_optB, inpads_opt, w_opt, Act, CalDelta, outc, 0, &err, &err2); + ae2fCL_mAnnSlpInit(&rtn->CL_Slp, incs_optA, ginc_optB, inpads_opt, w_opt, Act, CalDelta, outc, 0, &err, &err2); if(err_opt) *err_opt = err; if(err_nfound_opt) *err_nfound_opt = err2; diff --git a/test/cl-src/SlpLeak.c b/test/cl-src/SlpLeak.c new file mode 100644 index 0000000..6f23335 --- /dev/null +++ b/test/cl-src/SlpLeak.c @@ -0,0 +1,19 @@ +#include + +ae2f_float_t a(ae2f_float_t) { return 0; } +ae2f_float_t b(ae2f_float_t, ae2f_float_t) { return 0; } + +int main() { + ae2fCL_AnnSlp* slp; + + slp = ae2fCL_AnnSlpMkB( + 1, 0, 0, a, b, 1, 0, 0, 0 + ); + ae2f_float_t in, out; + + ae2f_mAnnSlpPredict(&slp->Slp, &in, &out); + ae2f_mAnnSlpTrainB(&slp->Slp, &in, &out, 0.1); + ae2fCL_AnnSlpDel(slp); + + return 0; +} \ No newline at end of file diff --git a/test/cl-src/SlpMk.cc b/test/cl-src/SlpMk.cc index 2e3504d..fbae68c 100644 --- a/test/cl-src/SlpMk.cc +++ b/test/cl-src/SlpMk.cc @@ -24,7 +24,7 @@ int mainc() { }; ae2f_mAnnSp* Perc = 0; - ae2f_mAnnSlp* SLP; + ae2fCL_AnnSlp* SLP; ae2f_float_t out_checksum = 0; err = ae2fCL_AnnMkEasy(errcl); @@ -36,10 +36,10 @@ int mainc() { &err, errcl ); - Perc = ae2f_mAnnSlpPerV(SLP, 0); + Perc = ae2f_mAnnSlpPerV(&SLP->Slp, 0); CHECK_ERR(err, CL_SUCCESS, __failure); - err = SLP->Predict(Buff, &outfloat); + err = ae2f_mAnnSlpPredict(&SLP->Slp, Buff, &outfloat); if(err) goto __failure; printf("out: %f\n", outfloat); @@ -65,7 +65,7 @@ int mainc() { } __failure: - if(SLP) ae2f_mAnnSlpDel(SLP); + if(SLP) ae2fCL_AnnSlpDel(SLP); ae2fCL_AnnDel(); if(ae2fCL_Ann.Q) clReleaseCommandQueue(ae2fCL_Ann.Q); if(ae2fCL_Ann.Ctx) clReleaseContext(ae2fCL_Ann.Ctx); @@ -81,7 +81,7 @@ int maincc() { }; ae2f_mAnnSp* Perc = 0; - ae2f_mAnnSlp* SLP; + ae2fCL_AnnSlp* SLP; ae2f_float_t out_checksum = 0; err = ae2fCL_AnnMkEasy(errcl); @@ -93,10 +93,10 @@ int maincc() { &err, errcl ); - Perc = ae2f_mAnnSlpPerV(SLP, 0); + Perc = SLP->Slp.Perc(0); CHECK_ERR(err, CL_SUCCESS, __failure); - err = SLP->Predict(Buff, &outfloat); + err = SLP->Slp.Predict(Buff, &outfloat); if(err) goto __failure; printf("out: %f\n", outfloat); @@ -122,7 +122,7 @@ int maincc() { } __failure: - if(SLP) ae2f_mAnnSlpDel(SLP); + if(SLP) delete SLP; ae2fCL_AnnDel(); if(ae2fCL_Ann.Q) clReleaseCommandQueue(ae2fCL_Ann.Q); if(ae2fCL_Ann.Ctx) clReleaseContext(ae2fCL_Ann.Ctx); diff --git a/test/cl-src/SlpTrainAND.c b/test/cl-src/SlpTrainAND.c index 1e60dab..fbcbc4e 100644 --- a/test/cl-src/SlpTrainAND.c +++ b/test/cl-src/SlpTrainAND.c @@ -42,16 +42,16 @@ int main() { err = ae2fCL_AnnMkEasy(&errcl); CHECK_ERR(err, CL_SUCCESS, __failure); - ae2f_mAnnSlp* Slp = ae2fCL_AnnSlpMkB( + ae2fCL_AnnSlp* Slp = ae2fCL_AnnSlpMkB( 2, 0, 0, Forward, Backward, 1, 0, &err2, &errcl ); ae2f_float_t outbuff[2] = { 5 }; - #define ae2fCL_AnnSlpPredict(obj, inb, _, in_idx, __, out, ...) \ - ae2f_mAnnSlpPredict(obj, inb + in_idx, out) + #define ae2fCL_mAnnSlpPredict(obj, inb, _, in_idx, __, out, ...) \ + ae2f_mAnnSlpPredict(&obj->Slp, inb + in_idx, out) - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 0/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -59,7 +59,7 @@ int main() { err = err2; goto __failure; } printf("Checking the value: %f (1, 1)\n", outbuff[0]); - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 6/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -67,7 +67,7 @@ int main() { err = err2; goto __failure; } printf("Checking the value: %f (0, 0)\n", outbuff[0]); - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 4/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -75,7 +75,7 @@ int main() { err = err2; goto __failure; } printf("Checking the value: %f (0, 1)\n", outbuff[0]); - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 2/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -86,12 +86,12 @@ int main() { if(err2) { err = err2; goto __failure; } - #define ae2fCL_AnnSlpTrain(obj, ins, _, in_idx, __, goal, learnrate, ...) ae2f_mAnnSlpTrainB(obj, ins + in_idx, goal, learnrate) + #define ae2fCL_mAnnSlpTrain(obj, ins, _, in_idx, __, goal, learnrate, ...) ae2f_mAnnSlpTrainB(obj, ins + in_idx, goal, learnrate) if(err) goto __failure; for(size_t _ = 0; _ < gEpochs; _++) { err2 = ae2f_mAnnSlpTrainB( - Slp, ins, + &Slp->Slp, ins, goals + 0, gLearningRate ); if(err2) { @@ -102,7 +102,7 @@ int main() { } err2 = ae2f_mAnnSlpTrainB( - Slp, ins + 2/*in_idx*/, + &Slp->Slp, ins + 2/*in_idx*/, goals + 2, gLearningRate ); if(err2) { @@ -110,7 +110,7 @@ int main() { } err2 = ae2f_mAnnSlpTrainB( - Slp, ins + 4/*in_idx*/, + &Slp->Slp, ins + 4/*in_idx*/, goals + 2, gLearningRate ); if(err2) { @@ -118,7 +118,7 @@ int main() { } err2 = ae2f_mAnnSlpTrainB( - Slp, ins + 6/*in_idx*/, + &Slp->Slp, ins + 6/*in_idx*/, goals + 2, gLearningRate ); if(err2) { @@ -128,7 +128,7 @@ int main() { puts("we are going for predict"); - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 0/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -140,7 +140,7 @@ int main() { err = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 6/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -148,14 +148,14 @@ int main() { err = err2; goto __failure; } printf("Checking the value: %f\n", outbuff[0]); if(outbuff[0] > 0.5) { - err2 = ae2f_mAnnSpPredict(ae2f_mAnnSlpPerV(Slp, 0), ins + 6, outbuff); + err2 = ae2f_mAnnSpPredict(ae2f_mAnnSlpPerV(&Slp->Slp, 0), ins + 6, outbuff); printf("Checking the value (SP): %f\n", outbuff[0]); printf("AND 0, 0 no good\n"); err = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 4/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -167,7 +167,7 @@ int main() { err = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 2/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -180,7 +180,7 @@ int main() { } __failure: - ae2f_mAnnSlpDel(Slp); + ae2f_AnnSlpDel(Slp); ae2fCL_AnnDel(); printf("ERR: %d\n", err); return err; diff --git a/test/cl-src/SpTrainAND.c b/test/cl-src/SpTrainAND.c index 31b96af..ebe3559 100644 --- a/test/cl-src/SpTrainAND.c +++ b/test/cl-src/SpTrainAND.c @@ -55,7 +55,7 @@ int main() { if(err2) { err = err2; goto __failure; } - #define ae2fCL_AnnSlpTrain(obj, ins, _, in_idx, __, goal, learnrate, ...) ae2f_mAnnSpTrainB(obj, ins + in_idx, *(goal), learnrate) + #define ae2fCL_mAnnSlpTrain(obj, ins, _, in_idx, __, goal, learnrate, ...) ae2f_mAnnSpTrainB(obj, ins + in_idx, *(goal), learnrate) if(err) goto __failure; for(size_t _ = 0; _ < gEpochs; _++) { @@ -67,7 +67,7 @@ int main() { err = err2; goto __failure; } - err2 = ae2fCL_AnnSlpTrain( + err2 = ae2fCL_mAnnSlpTrain( &Perc->Sp, ins, 0, 2/*in_idx*/, 0, goals + 2, gLearningRate, @@ -78,7 +78,7 @@ int main() { err = err2; goto __failure; } - err2 = ae2fCL_AnnSlpTrain( + err2 = ae2fCL_mAnnSlpTrain( &Perc->Sp, ins, 0, 4/*in_idx*/, 0, goals + 2, gLearningRate, @@ -89,7 +89,7 @@ int main() { err = err2; goto __failure; } - err2 = ae2fCL_AnnSlpTrain( + err2 = ae2fCL_mAnnSlpTrain( &Perc->Sp, ins, 0, 6/*in_idx*/, 0, goals + 2, gLearningRate, @@ -102,10 +102,10 @@ int main() { } ae2f_float_t outbuff[2] = { 5 }; - #define ae2fCL_AnnSlpPredict(obj, inb, _, in_idx, __, out, ...) \ + #define ae2fCL_mAnnSlpPredict(obj, inb, _, in_idx, __, out, ...) \ ae2f_mAnnSpPredict(obj, inb + in_idx, (out)) - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( &Perc->Sp, ins, 0, 0/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -117,7 +117,7 @@ int main() { err = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( &Perc->Sp, ins, 0, 6/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -129,7 +129,7 @@ int main() { err = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( &Perc->Sp, ins, 0, 4/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -141,7 +141,7 @@ int main() { err = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( &Perc->Sp, ins, 0, 2/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context diff --git a/test/src/SlpTrainAND.cc b/test/src/SlpTrainAND.cc index 0956806..c9c6db2 100644 --- a/test/src/SlpTrainAND.cc +++ b/test/src/SlpTrainAND.cc @@ -46,10 +46,10 @@ int mainc() { ); ae2f_float_t outbuff[2] = { 5 }; - #define ae2fCL_AnnSlpPredict(obj, inb, _, in_idx, __, out, ...) \ + #define ae2fCL_mAnnSlpPredict(obj, inb, _, in_idx, __, out, ...) \ ae2f_mAnnSlpPredict(&obj->Slp, inb + in_idx, out) - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 0/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -57,7 +57,7 @@ int mainc() { err = err2; goto __failure; } printf("Checking the value: %f (1, 1)\n", outbuff[0]); - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 6/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -65,7 +65,7 @@ int mainc() { err = err2; goto __failure; } printf("Checking the value: %f (0, 0)\n", outbuff[0]); - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 4/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -73,7 +73,7 @@ int mainc() { err = err2; goto __failure; } printf("Checking the value: %f (0, 1)\n", outbuff[0]); - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 2/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -124,7 +124,7 @@ int mainc() { puts("we are going for predict"); - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 0/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -136,7 +136,7 @@ int mainc() { err = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 6/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -151,7 +151,7 @@ int mainc() { err = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 4/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -163,7 +163,7 @@ int mainc() { err = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 2/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -203,11 +203,11 @@ int maincc() { ); ae2f_float_t outbuff[2] = { 5 }; - #undef ae2fCL_AnnSlpPredict - #define ae2fCL_AnnSlpPredict(obj, inb, _, in_idx, __, out, ...) \ + #undef ae2fCL_mAnnSlpPredict + #define ae2fCL_mAnnSlpPredict(obj, inb, _, in_idx, __, out, ...) \ (obj)->Slp.Predict(inb + in_idx, out) // ae2f_mAnnSlpPredict(obj, inb + in_idx, out) - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 0/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -215,7 +215,7 @@ int maincc() { err = err2; goto __failure; } printf("Checking the value: %f (1, 1)\n", outbuff[0]); - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 6/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -223,7 +223,7 @@ int maincc() { err = err2; goto __failure; } printf("Checking the value: %f (0, 0)\n", outbuff[0]); - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 4/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -231,7 +231,7 @@ int maincc() { err = err2; goto __failure; } printf("Checking the value: %f (0, 1)\n", outbuff[0]); - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 2/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -243,8 +243,8 @@ int maincc() { err = err2; goto __failure; } - #undef ae2fCL_AnnSlpTrain - #define ae2fCL_AnnSlpTrain(obj, ins, _, in_idx, __, goal, learnrate, ...) \ + #undef ae2fCL_mAnnSlpTrain + #define ae2fCL_mAnnSlpTrain(obj, ins, _, in_idx, __, goal, learnrate, ...) \ (obs)->Slp.TrainB(ins + in_idx, goal, learnrate) // ae2f_mAnnSlpTrainB(obj, ins + in_idx, goal, learnrate) if(err) goto __failure; @@ -283,7 +283,7 @@ int maincc() { puts("we are going for predict"); - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 0/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -295,7 +295,7 @@ int maincc() { err = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 6/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -310,7 +310,7 @@ int maincc() { err = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 4/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -322,7 +322,7 @@ int maincc() { err = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 2/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context diff --git a/test/src/SpTrainAND.c b/test/src/SpTrainAND.c index 6fe4b05..bf99e25 100644 --- a/test/src/SpTrainAND.c +++ b/test/src/SpTrainAND.c @@ -52,7 +52,7 @@ int main() { if(err2) { err = err2; goto __failure; } - #define ae2fCL_AnnSlpTrain(obj, ins, _, in_idx, __, goal, learnrate, ...) ae2f_mAnnSpTrainB(obj, ins + in_idx, *(goal), learnrate) + #define ae2fCL_mAnnSlpTrain(obj, ins, _, in_idx, __, goal, learnrate, ...) ae2f_mAnnSpTrainB(obj, ins + in_idx, *(goal), learnrate) if(err) goto __failure; for(size_t _ = 0; _ < gEpochs; _++) { @@ -64,7 +64,7 @@ int main() { err = err2; goto __failure; } - err2 = ae2fCL_AnnSlpTrain( + err2 = ae2fCL_mAnnSlpTrain( Slp, ins, 0, 2/*in_idx*/, 0, goals + 2, gLearningRate, @@ -75,7 +75,7 @@ int main() { err = err2; goto __failure; } - err2 = ae2fCL_AnnSlpTrain( + err2 = ae2fCL_mAnnSlpTrain( Slp, ins, 0, 4/*in_idx*/, 0, goals + 2, gLearningRate, @@ -86,7 +86,7 @@ int main() { err = err2; goto __failure; } - err2 = ae2fCL_AnnSlpTrain( + err2 = ae2fCL_mAnnSlpTrain( Slp, ins, 0, 6/*in_idx*/, 0, goals + 2, gLearningRate, @@ -99,10 +99,10 @@ int main() { } ae2f_float_t outbuff[2] = { 5 }; - #define ae2fCL_AnnSlpPredict(obj, inb, _, in_idx, __, out, ...) \ + #define ae2fCL_mAnnSlpPredict(obj, inb, _, in_idx, __, out, ...) \ ae2f_mAnnSpPredict(obj, inb + in_idx, (out)) - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 0/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -114,7 +114,7 @@ int main() { err = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 6/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -126,7 +126,7 @@ int main() { err = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 4/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -138,7 +138,7 @@ int main() { err = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 2/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context From ed935792fe5ab39b60429b3bc86f01b07c384c76 Mon Sep 17 00:00:00 2001 From: Yui San ae2f Date: Thu, 13 Mar 2025 10:18:37 +0000 Subject: [PATCH 07/13] Create cmake-multi-platform.yml --- .github/workflows/cmake-multi-platform.yml | 75 ++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 .github/workflows/cmake-multi-platform.yml diff --git a/.github/workflows/cmake-multi-platform.yml b/.github/workflows/cmake-multi-platform.yml new file mode 100644 index 0000000..7ab1b4d --- /dev/null +++ b/.github/workflows/cmake-multi-platform.yml @@ -0,0 +1,75 @@ +# This starter workflow is for a CMake project running on multiple platforms. There is a different starter workflow if you just want a single platform. +# See: https://github.com/actions/starter-workflows/blob/main/ci/cmake-single-platform.yml +name: CMake on multiple platforms + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + build: + runs-on: ${{ matrix.os }} + + strategy: + # Set fail-fast to false to ensure that feedback is delivered for all matrix combinations. Consider changing this to true when your workflow is stable. + fail-fast: false + + # Set up a matrix to run the following 3 configurations: + # 1. + # 2. + # 3. + # + # To add more build types (Release, Debug, RelWithDebInfo, etc.) customize the build_type list. + matrix: + os: [ubuntu-latest, windows-latest] + build_type: [Release] + c_compiler: [gcc, clang, cl] + include: + - os: windows-latest + c_compiler: cl + cpp_compiler: cl + - os: ubuntu-latest + c_compiler: gcc + cpp_compiler: g++ + - os: ubuntu-latest + c_compiler: clang + cpp_compiler: clang++ + exclude: + - os: windows-latest + c_compiler: gcc + - os: windows-latest + c_compiler: clang + - os: ubuntu-latest + c_compiler: cl + + steps: + - uses: actions/checkout@v4 + + - name: Set reusable strings + # Turn repeated input strings (such as the build output directory) into step outputs. These step outputs can be used throughout the workflow file. + id: strings + shell: bash + run: | + echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT" + + - name: Configure CMake + # Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make. + # See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type + run: > + cmake -B ${{ steps.strings.outputs.build-output-dir }} + -DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }} + -DCMAKE_C_COMPILER=${{ matrix.c_compiler }} + -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} + -S ${{ github.workspace }} + + - name: Build + # Build your program with the given configuration. Note that --config is needed because the default Windows generator is a multi-config generator (Visual Studio generator). + run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }} + + - name: Test + working-directory: ${{ steps.strings.outputs.build-output-dir }} + # Execute tests defined by the CMake configuration. Note that --build-config is needed because the default Windows generator is a multi-config generator (Visual Studio generator). + # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail + run: ctest --build-config ${{ matrix.build_type }} From 127f077fc6c5f72f9819260ae83633b98f4affef Mon Sep 17 00:00:00 2001 From: Yui San ae2f Date: Thu, 13 Mar 2025 10:30:12 +0000 Subject: [PATCH 08/13] Update cmake-multi-platform.yml --- .github/workflows/cmake-multi-platform.yml | 129 +++++++++++---------- 1 file changed, 65 insertions(+), 64 deletions(-) diff --git a/.github/workflows/cmake-multi-platform.yml b/.github/workflows/cmake-multi-platform.yml index 7ab1b4d..5c8ec22 100644 --- a/.github/workflows/cmake-multi-platform.yml +++ b/.github/workflows/cmake-multi-platform.yml @@ -1,75 +1,76 @@ -# This starter workflow is for a CMake project running on multiple platforms. There is a different starter workflow if you just want a single platform. -# See: https://github.com/actions/starter-workflows/blob/main/ci/cmake-single-platform.yml -name: CMake on multiple platforms +name: Build with OpenCL on Ubuntu and Windows -on: - push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] +on: [push, pull_request] jobs: - build: - runs-on: ${{ matrix.os }} + build-ubuntu: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Install OpenCL + run: | + sudo apt-get update + sudo apt-get install -y ocl-icd-opencl-dev pocl-opencl-icd + + - name: Verify OpenCL installation + run: | + clinfo # Lists available OpenCL platforms and devices - strategy: - # Set fail-fast to false to ensure that feedback is delivered for all matrix combinations. Consider changing this to true when your workflow is stable. - fail-fast: false + - name: Build project + run: | + # Placeholder: Replace with your build commands (e.g., make, cmake) + rm -r -f build + cd build + cmake .. + cmake --build . + cd .. - # Set up a matrix to run the following 3 configurations: - # 1. - # 2. - # 3. - # - # To add more build types (Release, Debug, RelWithDebInfo, etc.) customize the build_type list. - matrix: - os: [ubuntu-latest, windows-latest] - build_type: [Release] - c_compiler: [gcc, clang, cl] - include: - - os: windows-latest - c_compiler: cl - cpp_compiler: cl - - os: ubuntu-latest - c_compiler: gcc - cpp_compiler: g++ - - os: ubuntu-latest - c_compiler: clang - cpp_compiler: clang++ - exclude: - - os: windows-latest - c_compiler: gcc - - os: windows-latest - c_compiler: clang - - os: ubuntu-latest - c_compiler: cl + - name: Run tests + run: | + cd build + ctest + cd .. + build-windows: + runs-on: windows-latest steps: - - uses: actions/checkout@v4 + - name: Checkout code + uses: actions/checkout@v4 - - name: Set reusable strings - # Turn repeated input strings (such as the build output directory) into step outputs. These step outputs can be used throughout the workflow file. - id: strings - shell: bash - run: | - echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT" + - name: Install OpenCL Runtime (Intel CPU) + shell: pwsh + run: | + # Download Intel OpenCL CPU runtime (update URL if needed) + $url = "https://downloadmirror.intel.com/736607/opencl_runtime_2023.16.10.0_x64_setup.msi" + $output = "opencl_setup.msi" + Invoke-WebRequest -Uri $url -OutFile $output + # Install silently + Start-Process msiexec -ArgumentList "/i $output /quiet /norestart" -Wait -NoNewWindow + Write-Host "OpenCL runtime installed" - - name: Configure CMake - # Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make. - # See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type - run: > - cmake -B ${{ steps.strings.outputs.build-output-dir }} - -DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }} - -DCMAKE_C_COMPILER=${{ matrix.c_compiler }} - -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} - -S ${{ github.workspace }} + - name: Verify OpenCL setup + shell: pwsh + run: | + # Placeholder: Add a verification step if you have a tool/test program + Write-Host "OpenCL runtime should be available (no clinfo on Windows by default)" - - name: Build - # Build your program with the given configuration. Note that --config is needed because the default Windows generator is a multi-config generator (Visual Studio generator). - run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }} + - name: Build project + shell: cmd + run: | + # Placeholder: Replace with your build commands (e.g., MSBuild, cmake) + echo Building OpenCL project on Windows... + cd build + cmake .. + cmake --build . + cd .. - - name: Test - working-directory: ${{ steps.strings.outputs.build-output-dir }} - # Execute tests defined by the CMake configuration. Note that --build-config is needed because the default Windows generator is a multi-config generator (Visual Studio generator). - # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail - run: ctest --build-config ${{ matrix.build_type }} + - name: Run tests + shell: cmd + run: | + # Placeholder: Replace with your test commands + echo Running tests on Windows... + cd build + ctest + cd .. From d6ba063e3b1c9b6edf4b10a22240dc965a983e6e Mon Sep 17 00:00:00 2001 From: Yui San ae2f Date: Thu, 13 Mar 2025 10:34:37 +0000 Subject: [PATCH 09/13] Update cmake-multi-platform.yml --- .github/workflows/cmake-multi-platform.yml | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/.github/workflows/cmake-multi-platform.yml b/.github/workflows/cmake-multi-platform.yml index 5c8ec22..a12e2b9 100644 --- a/.github/workflows/cmake-multi-platform.yml +++ b/.github/workflows/cmake-multi-platform.yml @@ -21,6 +21,7 @@ jobs: - name: Build project run: | # Placeholder: Replace with your build commands (e.g., make, cmake) + chdir /home/runner/work/ae2f/CL-Ann rm -r -f build cd build cmake .. @@ -29,7 +30,7 @@ jobs: - name: Run tests run: | - cd build + chdir /home/runner/work/ae2f/CL-Ann/build ctest cd .. @@ -60,17 +61,18 @@ jobs: shell: cmd run: | # Placeholder: Replace with your build commands (e.g., MSBuild, cmake) + chdir C:/Users/runneradmin/work/ae2f/CL-Ann echo Building OpenCL project on Windows... - cd build + chdir build cmake .. cmake --build . - cd .. + chdir .. - name: Run tests shell: cmd run: | # Placeholder: Replace with your test commands echo Running tests on Windows... - cd build + chdir C:/Users/runneradmin/work/ae2f/CL-Ann/build ctest - cd .. + chdir .. From aa841f15eb5de093d07dc7de6fa87a30dfba2ff3 Mon Sep 17 00:00:00 2001 From: Yui San ae2f Date: Thu, 13 Mar 2025 10:36:01 +0000 Subject: [PATCH 10/13] Update cmake-multi-platform.yml --- .github/workflows/cmake-multi-platform.yml | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/.github/workflows/cmake-multi-platform.yml b/.github/workflows/cmake-multi-platform.yml index a12e2b9..c6bf769 100644 --- a/.github/workflows/cmake-multi-platform.yml +++ b/.github/workflows/cmake-multi-platform.yml @@ -14,10 +14,6 @@ jobs: sudo apt-get update sudo apt-get install -y ocl-icd-opencl-dev pocl-opencl-icd - - name: Verify OpenCL installation - run: | - clinfo # Lists available OpenCL platforms and devices - - name: Build project run: | # Placeholder: Replace with your build commands (e.g., make, cmake) @@ -51,12 +47,6 @@ jobs: Start-Process msiexec -ArgumentList "/i $output /quiet /norestart" -Wait -NoNewWindow Write-Host "OpenCL runtime installed" - - name: Verify OpenCL setup - shell: pwsh - run: | - # Placeholder: Add a verification step if you have a tool/test program - Write-Host "OpenCL runtime should be available (no clinfo on Windows by default)" - - name: Build project shell: cmd run: | From 35823f9ec83974a7e9e02fd72a8545edfad5c95b Mon Sep 17 00:00:00 2001 From: Yui San ae2f Date: Thu, 13 Mar 2025 10:37:41 +0000 Subject: [PATCH 11/13] Update cmake-multi-platform.yml --- .github/workflows/cmake-multi-platform.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/cmake-multi-platform.yml b/.github/workflows/cmake-multi-platform.yml index c6bf769..db95081 100644 --- a/.github/workflows/cmake-multi-platform.yml +++ b/.github/workflows/cmake-multi-platform.yml @@ -17,7 +17,7 @@ jobs: - name: Build project run: | # Placeholder: Replace with your build commands (e.g., make, cmake) - chdir /home/runner/work/ae2f/CL-Ann + cd /home/runner/work/ae2f/CL-Ann rm -r -f build cd build cmake .. @@ -26,7 +26,7 @@ jobs: - name: Run tests run: | - chdir /home/runner/work/ae2f/CL-Ann/build + cd /home/runner/work/ae2f/CL-Ann/build ctest cd .. From cd134c437bac2363f14cd776a815ddb72feec62c Mon Sep 17 00:00:00 2001 From: Yui San ae2f Date: Thu, 13 Mar 2025 10:39:49 +0000 Subject: [PATCH 12/13] Update cmake-multi-platform.yml --- .github/workflows/cmake-multi-platform.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/cmake-multi-platform.yml b/.github/workflows/cmake-multi-platform.yml index db95081..7d79a89 100644 --- a/.github/workflows/cmake-multi-platform.yml +++ b/.github/workflows/cmake-multi-platform.yml @@ -8,6 +8,7 @@ jobs: steps: - name: Checkout code uses: actions/checkout@v4 + run: ls - name: Install OpenCL run: | From 5d730dca6f23aef36dc81448aa49a05765bf5278 Mon Sep 17 00:00:00 2001 From: Yui San ae2f Date: Thu, 13 Mar 2025 10:41:29 +0000 Subject: [PATCH 13/13] Delete .github/workflows/cmake-multi-platform.yml --- .github/workflows/cmake-multi-platform.yml | 69 ---------------------- 1 file changed, 69 deletions(-) delete mode 100644 .github/workflows/cmake-multi-platform.yml diff --git a/.github/workflows/cmake-multi-platform.yml b/.github/workflows/cmake-multi-platform.yml deleted file mode 100644 index 7d79a89..0000000 --- a/.github/workflows/cmake-multi-platform.yml +++ /dev/null @@ -1,69 +0,0 @@ -name: Build with OpenCL on Ubuntu and Windows - -on: [push, pull_request] - -jobs: - build-ubuntu: - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v4 - run: ls - - - name: Install OpenCL - run: | - sudo apt-get update - sudo apt-get install -y ocl-icd-opencl-dev pocl-opencl-icd - - - name: Build project - run: | - # Placeholder: Replace with your build commands (e.g., make, cmake) - cd /home/runner/work/ae2f/CL-Ann - rm -r -f build - cd build - cmake .. - cmake --build . - cd .. - - - name: Run tests - run: | - cd /home/runner/work/ae2f/CL-Ann/build - ctest - cd .. - - build-windows: - runs-on: windows-latest - steps: - - name: Checkout code - uses: actions/checkout@v4 - - - name: Install OpenCL Runtime (Intel CPU) - shell: pwsh - run: | - # Download Intel OpenCL CPU runtime (update URL if needed) - $url = "https://downloadmirror.intel.com/736607/opencl_runtime_2023.16.10.0_x64_setup.msi" - $output = "opencl_setup.msi" - Invoke-WebRequest -Uri $url -OutFile $output - # Install silently - Start-Process msiexec -ArgumentList "/i $output /quiet /norestart" -Wait -NoNewWindow - Write-Host "OpenCL runtime installed" - - - name: Build project - shell: cmd - run: | - # Placeholder: Replace with your build commands (e.g., MSBuild, cmake) - chdir C:/Users/runneradmin/work/ae2f/CL-Ann - echo Building OpenCL project on Windows... - chdir build - cmake .. - cmake --build . - chdir .. - - - name: Run tests - shell: cmd - run: | - # Placeholder: Replace with your test commands - echo Running tests on Windows... - chdir C:/Users/runneradmin/work/ae2f/CL-Ann/build - ctest - chdir ..