⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userext.tex

📁 basic.c */ /**//* Project:NeuroBasic, basic package*//**/ /* Survey:This is a simple Basic b-code
💻 TEX
📖 第 1 页 / 共 4 页
字号:
%======================Neuro-objects are defined with a C structure which containsinformation about the object itself and pointers to the object-data(\eg layers, weight matrices, etc.). All neuro-object structures areunited in a union called {\tt neuro\_obj\_t}. An array of these unionscalled {\tt neuro\_objs}, finally, holds the actual neuro-objects.The following example structure defines a layer for theback-propagation algorithm.\begin{verbatim}  typedef struct  {    MFLOAT      *poutp;         /* pointer to output vector */    MFLOAT      *pdelta;        /* pointer to error vector */    comm_def_t  window;         /* communication window */  } layer_t;                    /* general layer */\end{verbatim}\noindent In this case, the communication window contains allnecessary information about the layer, like its size and partitioningamong the processing elements.  To create a new neuro-object type, the programmer has to put thecorresponding type definition into the file {\tt nobjs.h} of the localsubdirectory of the package it belongs to. The global makefilethen calls the utility program {\tt mknobjs} which reads the {\ttnobjs.h} files of all subdirectories and creates the file {\ttbasic/allnobjs.h} which contains the union {\tt neuro\_obj\_t}. Each C module which works with neuro-objects must include this headerfile.Note that the size of a neuro-object type is limited to 160 bytes({\tt MINT}s and {\tt MFLOAT}s both need 4 bytes per value). Note alsothat one communication window ({\tt comm\_def\_t}) already needs 72bytes.In order to recognize neuro-objects {\tt mknobjs} assumes that thecorresponding structure names end with the letters ``{\tt \_t}''. Itdrops these two letters when creating the corresponding object-name inthe union. The following example shows the union for containing theabove layer-type.\begin{verbatim}  typedef union  {        .        .    layer_t layer;        .        .    public_t public;  } neuro_obj_t;\end{verbatim}\noindent Let's assume that element 3 in the {\tt neuro\_objs} arrayis of our layer type and we want to access the fifth element of theoutput layer. The corresponding C expression to do this is\begin{verbatim}  neuro_objs[3].layer.poutp[4]\end{verbatim}\noindent The structure {\tt public\_t} is the largest type of theunion and determines its size. It reserves 160 byte at the top ({\ttfiller}) and defines afterwards variables for public information forthe neuro-objects. The reserved space at the top is to store theinformation of the other neuro-object types. The {\tt public\_t} isdefined as follows:\begin{verbatim}  typedef struct  {    MINT filler[40];    MINT obtype;    MINT trans_buf_size;    void (*fn_release)(MINT nobj);    MINT (*fn_load)(MINT nobj);    MINT (*fn_save)(MINT nobj);    MINT (*fn_nput)(MINT nobj, MINT nx, MINT ny, MFLOAT value);    MFLOAT (*fn_nget)(MINT nobj, MINT nx, MINT ny);    MINT (*fn_rand)(MINT nobj, MFLOAT range);  } public_t;\end{verbatim}\noindent The structure elements have the following meaning:\begin{fctitem}{obtype}Each neuro-object type has a unique number to be recognized assuch. It is stored in this variable. The {\tt mknobjs} program createsa macro for each object type and writes it to the file {\ttbasic/allnobjs.h} (together with the neuro-object definitions). Themacro names are the capitalized object names preceded by the string{\tt N\_}. The type number of the {\tt layer\_t}, for instance, wouldbe {\tt N\_LAYER}.The number 0 is assigned to the predefined macro {\tt N\_FREE} tomark an element of the neuro-object list as unused.\end{fctitem}\begin{fctitem}{trans\_buf\_size}This is the minimum size of the transfer buffer (see Section~\ref{sec_transbuf}) used by this object. This variable isinternally used and must not be changed by the user.\end{fctitem}\begin{fctitem}{(*fn\_release)(MINT nobj)}This is a pointer to the release function of the neuro-object,\ie a function which frees all the allocated space of the objectand marks the object as unused by writing {\tt N\_FREE} into theobject type.\end{fctitem}\begin{fctitem}{(*fn\_load)(MINT nobj)}This is a pointer to the {\em load\/} function of the object. It willbe executed when the {\tt load()} function is called for thisobject from the Basic interpreter.n\end{fctitem}\begin{fctitem}{(*fn\_save)(MINT nobj)}This is a pointer to the {\em save\/} function of the object. It willbe executed when the {\tt save()} function is called for thisobject from the Basic interpreter.\end{fctitem}\begin{fctitem}{(*fn\_nput)(MINT nobj, MINT nx, MINT ny,MFLOAT value)} This is a pointer to the {\em put\/} function of theobject. It will be executed when the {\tt nput()} function is calledfor this object from the Basic interpreter.\end{fctitem}\begin{fctitem}{(*fn\_nget)(MINT nobj, MINT nx, MINT ny)}This is a pointer to the {\em get\/} function of the object. It willbe executed when the {\tt nget()} function is called for thisobject from the Basic interpreter.\end{fctitem}\begin{fctitem}{(*fn\_rand)(MINT nobj, MFLOAT range)}This is a pointer to the {\em randomize\/} function of the object. Itwill be executed when the {\tt randomize()} function is called forthis object from the Basic interpreter.\end{fctitem}\noindent All except the release function are voluntary. If theydon't exist for a certain object then the corresponding pointermust be {\tt NULL}.The following example C sequence tests if object 3 is of{\tt layer\_t} and, if yes, assigns the width of the layer fromits communication window to the variable {\tt width}.\begin{verbatim}  if (neuro_objs[3].public.type == N_LAYER)  {    width = neuro_objs[3].layer.window.dim.x;  }\end{verbatim}\subsection{Create Neuro-Objects}\index{create neuro-objects}%--------------------------------For each neuro-object type there must be a neuro-function to createobjects of its type. These functions usually start with the letters{\tt new\_} and end with the name of the object, \eg {\ttnew\_layer()} to create a new layer. The following steps have to betaken by the creator function:\begin{enumerate}\item Get the index of a free entry in the neuro-object list. This is  done by calling the function {\tt alloc\_nobj(tbsize)}  \index{alloc\_nobj@{\tt alloc\_nobj()}}. It returns the index or a  negative number if the object table is full. The parameter {\tt  tbsize} specifies the maximum space which the object ever will use  in the transfer buffer (see Section~\ref{sec_transbuf}) when  communicating its data. {\tt tbsize} is the number of machine  words. For 100 floating-point values, for instance, this would be  {\tt 100 * sizeof(MFLOAT)}.\\ In cases where it is necessary to  resize the transfer buffer, NeuroBasic provides the function {\tt  realloc\_nobj(nobj, tbsize)}   \index{realloc\_nobj@{\tt realloc\_nobj()}}. It sets the transfer  buffer size of the neuro object {\tt nobj} to the size {\tt tbsize}.\item Allocate the necessary memory space for the object and set  the pointers and other object specific variables accordingly.\item Set the object type to indicate that the object is no longer  free.\item Set the pointers to the release, load, save, nput and nget  function of this object. The release function is mandatory. The  others can be replaced by {\tt NULL} if not available.\item Return the index of the new object.\end{enumerate}\noindent The {\em release function\/} has to free all memory spacewhich was allocated for the object and then call {\tt free\_nobj(nobj)}to delete the object from the object table. The release function willget the index of the neuro-object as parameter and its return valuewill be passed to the Basic interpreter. The prototype of a releasefunction is\bigskip {\tt MINT {\em functionname}(MINT nobj);}\subsection{Load Functions}\index{load functions}%--------------------------Because a function running on \MUSIC\ cannot access the file system ofthe host computer, the file handling is done by the host part ofNeuroBasic. A load function just has to receive the data from thehost and store it at the right places of its neuro-object. Theprototype of a load function is\bigskip {\tt MINT {\em functionname}(MINT nobj);}\bigskip\noindent {\tt nobj} is the index of the object into which thedata has to be loaded. The return value of the function is passed tothe Basic interpreter.A load function first has to call the function {\tt setup\_load()}\index{setup\_load@{\tt setup\_load()}} which passes the expected  dimensions of the data to the host: \bigskip {\tt MINT setup\_load(ndim, xdim, ydim, zdim, block\_size,                               format);}\bigskip\noindent {\tt ndim} is the number of dimensions of the matrix(up to 3) followed by the three dimensions {\tt xdim}, {\tt ydim} and{\tt zdim}. An unused dimension (\eg {\tt zdim} when {\tt ndim}~=~2)has to be set to 1. The meaning of the individual dimensions is objectdependent and has to be coordinated among the neuro-functionsthemselves which work with the object.{\tt block\_size} is the size of the data which will bereceived in one block from the host. It can be set to {\tt xdim * ydim* zdim} if the complete data set will be loaded in one step. Note thatthe dimensions of a data block sent over the \MUSIC\ communicationnetwork are restricted in size which sometimes naturally splits theloading of a large data set in several blocks. The {\tt block\_size} isthe same throughout the complete loading procedure.{\tt format} indicates the format of the data to be loaded. It isalways floating-point ({\tt FF\_FLOAT32}). The only exception aretraining patterns which can load data in the compact fixed-pointformat ({\tt FF\_FIXED8}) to save memory space on \MUSIC.The {\tt setup\_load()} function returns 0 ({\tt FALSE}) if theloading process cannot be carried out. This is the case, for instance,when expected data dimensions for the neuro-object are not the same asstored on the file. In all other cases the function returns a nonzerovalue ({\tt TRUE}).To receive the actual data from the host computer either the {\ttRd\_from\_host()} function can be called or a normal communication canbe carried out with {\tt HOST\_IO} as the producer (see \MUSIC\Tutorial).\subsection{Save Functions}\index{save functions}%--------------------------A save function is the same as a load function except that the data ispassed in the other direction, \ie from the \MUSIC\ to the host, whichwill save it on disk. Again, the file handling is carried out by thehost part of NeuroBasic and a save function just has to send the dataof the desired object to the host. The prototype of a save function is\bigskip {\tt MINT {\em functionname}(MINT nobj);}\bigskip\noindent {\tt nobj} is the index of the object whichhas to be stored on disk. The return value of the function is passed tothe Basic interpreter.A save function first has to call the function {\tt setup\_save()}\index{setup\_save@{\tt setup\_save()}} which passes the dimensions ofthe data to the host: \bigskip {\tt MINT setup\_save(ndim, xdim, ydim, zdim, block\_size);}\bigskip\noindent {\tt ndim} is the number of dimensions of the matrix(up to 3) followed by the three dimensions {\tt xdim}, {\tt ydim} and{\tt zdim}. An unused dimension (\eg {\tt zdim} when {\tt ndim}~=~2)has to be set to 1. The meaning of the individual dimensions is objectdependent and has to be coordinated among the neuro-functionsthemselves which work with the object.{\tt block\_size} is the size of subblocks which will be sent to thehost in several steps. It can be set to {\tt xdim * ydim * zdim} ifthe complete data set will be sent in a single step. Note that thedimensions of a data block sent over the \MUSIC\ communication networkare restricted in size which sometimes naturally splits the saving ofa large data set in several blocks. The {\tt block\_size} is the samethroughout the complete saving procedure.The {\tt setup\_save()} function returns 0 ({\tt FALSE}) if the savingprocess cannot be carried out, for instance, if there is not enoughspace on the disk to store the data. In all other cases the functionreturns a nonzero value ({\tt TRUE}).To sent the actual data to the host computer either the {\ttWr\_to\_host()} function can be called or a normal communication canbe carried out with {\tt HOST\_IO} as the consumer (see \MUSIC\Tutorial).\subsection{Put Functions}\index{put functions}%-------------------------A put function has to receive a single value from the host and has tostore it at the right place of an object. The prototype of a putfunction is\bigskip {\tt MINT {\em functionname}(MINT nobj, MINT i, MINT j,                                      MFLOAT value);}\bigskip\noindent {\tt nobj} is the index of the object, {\tt i} and{\tt j} are ``coordinates'' which indicate the element in the objectwhere {\tt value} has to be put. The coordinates $i$ and $j$ are whatever the user types in when calling {\tt nput()}. Their interpretationis up to the put function itself. See the description of someneuro-objects in the previous chapters for examples.The return value of the put function is passed back to the Basicinterpreter.\subsection{Get Functions}\index{get functions}%-------------------------A get function is the opposite of a put function. It has to return thevalue of a specified element of an object. The prototype of a getfunction is\bigskip {\tt MFLOAT {\em functionname}(MINT nobj, MINT i, MINT j);}\bigskip\noindent {\tt nobj} is the index of the object, {\tt i} and{\tt j} are ``coordinates'' which indicate the element in the objectwhich has to be returned. The coordinates $i$ and $j$ are what everthe user types in when calling {\tt nput()}. Their interpretation isup to the get function itself. See the description of someneuro-objects in the previous chapters for examples.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -