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

📄 userext.tex

📁 basic.c */ /**//* Project:NeuroBasic, basic package*//**/ /* Survey:This is a simple Basic b-code
💻 TEX
📖 第 1 页 / 共 4 页
字号:
% *******************************************************************%% File:         userext.tex%% Survey:       NeuroBasic, Part of the Reference Manual%% Author:       Urs Mueller%               Electronics Laboratory, ETH Zuerich,%               Switzerland%% Created:      July 20, 1994% Modified:     November 29, 1994 (um)%% *******************************************************************\chapter{User Extensions}%************************This chapter describes how a programmer can extend the NeuroBasicsimulator with own neuro-functions. The reader should have a goodknowledge of the C\index{C programming language} programminglanguage\footnote{A good reference to the C programming language isBrian W. Kernighan and Dennis M. Ritchie's book {\em The C ProgrammingLanguage (ANSI C)}, second edition, Prentice Hall Software Series,1988.} and the \UNIX\index{UNIX@\UNIX} operating system.\footnote{Agood description of the UNIX operating system is in the book of BrianW. Kernighan and Rob Pike, {\em The UNIX Programming Environment},Prentice Hall Software Series, 1984.} There is little knowledge of the\MUSIC\ computer required to understand how to include own functionsinto the NeuroBasic environment. Only functions that should be able torun on the \MUSIC\index{music@\MUSIC} system have to be writtendata-parallel (see Chapter~\ref{sec_dataparallel}). However, most ofthe existing neuro-objects contain \MUSIC\ specific data and some ofthe aspects of the NeuroBasic software architecture become only clearunder consideration of the \MUSIC\ computer and its development tools.\section{Local and Parallel Version}%===================================Up to now we were talking of two versions of NeuroBasic, the local andthe \MUSIC\ version. In fact there is a third version, the simulatorversion which is used only in the development phase. The source codefor all three versions is the same. Just the compilers and the linkedlibraries differ for each version.Any parallel code written for \MUSIC\ can be compiled and executed ona single-processor computer as well, but, of course, not the other wayround. The three versions are described in the following.\paragraph{Local Version} \index{local version}The local version can be compiled for almost any workstation or{\small PC}. It's name is {\tt nbasic}. A simple simulator interface(in {\tt basic/simpsim.c}) makes the data-parallel code believe itruns on a ``parallel'' environment with only one processor. Allfunctions from the \MUSIC\ library are shadowed and perform the rightaction for the single-processor environment. Many of them, like {\ttWait\_data()}, just do nothing at all.Typically, the local version of NeuroBasic runs as fast as if the codewere directly written for a single-processor machine (no loss due tosimulation of the communication).\paragraph{MUSIC Version} \index{music version@\MUSIC\ version}This is the high-performance version. All neuro-objects are stored andall neuro-functions run parallel on the \MUSIC\ system. The Basicinterpreter itself runs on the host computer. To run the \MUSIC\version just run the host part by typing {\tt mubasic}. It willdownload the parallel code {\tt mubasic.lod} to the processingelements end execute it there.In fact, as mentioned earlier, it is not an interpreter but a compilerthat sends the compiled Basic code to all processing elements on the\MUSIC\ system where it is executed in parallel. This is to avoid thecommunication bottleneck between the host and \MUSIC.  However, sincethe compilation phase is typically a fraction of a second and istherefore not really noticeable to the user, we will refer to the hostpart of NeuroBasic as the Basic interpreter.User extensions contain only code that runs parallel on the \MUSIC\system. All code that runs on the host is part of the Basicinterpreter.\paragraph{Simulator Version} \index{simulator version}For \UNIX\ systems there exists a source-level simulator, called {\ttmusim}, \index{Musim} which simulates a parallel \MUSIC\ system withan arbitrary number of processing elements (see the Musimtutorial). It starts a task for the host part and an additional taskfor each processing element. These tasks run independently in paralleland {\tt musim} simulates the communication between them. Any \UNIX\debugger can be used to debug one or more of the tasks. Assembly codecan not be debugged in this way.The host part of the simulator version is called {\tt hbasic} (forhost part) and the \MUSIC part is called {\tt sbasic} (for slavepart). To simulate NeuroBasic with {\tt musim} one has to type\bigskip{\tt musim }$n${ \tt sbasic hbasic}\bigskip\noindent where $n$ is the number of processing elements.\section{Packages}\index{packages|(}%=================The NeuroBasic development environment is divided into {\empackages}. A package is a collection of neuro-functions andneuro-objects which belong to a certain algorithm or a class ofalgorithms. The back-propagation package, for example, contains theobjects {\em layer\/}, {\em fully connected weight sets\/}, thefunctions {\tt fcprop()}, {\tt fcbackprop()} and more. It is simple toadd packages to or remove packages from NeuroBasic.The Basic interpreter itself is just another package. It is thereforeeasy to share or exchange packages among users or to update anexisting installation with the latest version of the Basicinterpreter. One also can save program space by removing unusedpackages from NeuroBasic. This possibility is important if thesimulator runs on the \MUSIC\ system. The program memory of itsprocessing elements is restricted to 131 or 524~kByte (depending onthe hardware version) which can be too small to hold all availableNeuroBasic packages.Each package has a subdirectory where all its files (source code,object modules, etc.) are stored. Such a subdirectory is a smalldevelopment environment in itself. It has, for instance, its own {\ttmakefile} that compiles and links all of its code into a single objectmodule. In the parent directory there is a {\em global makefile\/}\index{makefile!global} which builds the complete NeuroBasic programby subsequently executing the local makefiles and then linking theresulting object modules of all subdirectories together to anexecutable program.All packages that should be included are listed in a macro called {\ttPACKAGES} defined in the file {\tt macros.mk}. This file will beincluded by the global makefile. A package can therefore be added orremoved by simply editing the corresponding line at the beginning ofthe global makefile. To build a NeuroBasic version with the packages{\em backprop} and {\em example}, the corresponding place of theglobal makefile would look as follows:\begin{verbatim}  PACKAGES = backprop example\end{verbatim}\noindent The {\em basic\/} package does not appear in that listbecause it cannot be removed.To regulate the interfacing mechanism between the NeuroBasicinterpreter and its packages the subdirectory of each package mustcontain the following standardized files:\begin{fctitem}{nfcts.h}This file contains prototypes of all functions of the package thatshould be ``visible'' from the Basic interpreter. The global makefilewill use this information to create a function list and interface codefor the Basic interpreter.\end{fctitem}\begin{fctitem}{nobjs.h}This file contains the type definitions of all neuro-object types andthe symbolic constants \index{symbolic constants} of a package..\end{fctitem}\begin{fctitem}{manual.tex}This file contains the description of the neuro-objects andneuro-functions of the package in \LaTeX\ format. The global make filewill collect the {\tt manual.tex} files from all packages and mergethem to a complete NeuroBasic documentation. A local {\tt manual.tex}file can be empty but it must be there.\end{fctitem}\begin{fctitem}{makefile}This is the {\em local\/} makefile of the package. It must be able tobuild object modules of the different program versions (\eg local or\MUSIC\ version) and to make a backup of its complete source code.The global makefile will link the object modules of all packages intoa final executable program.\end{fctitem}\index{packages|)}\subsection{Utility Packages}%----------------------------Utility packages contain programs which are useful for but not a partof NeuroBasic. They exist only as a ``local'' version (no \MUSIC\version) and their description will go into the appendix of themanual. A subdirectory containing a utility package must have a {\ttmakefile} and a documentation file {\tt manual.tex}.The utility packages included into a NeuroBasic version are specifiedwith the macro {\tt UTILITY} in the file {\tt macros.mk} (next to{\tt PACKAGES}).\section{Neuro-Functions}\index{neuro-functions|(}%========================Neuro-functions are normal functions written in C or assemblylanguage. They can be called from the NeuroBasic interpreter via ahidden interface mechanism. Their return values are restricted to thetypes {\tt MINT} {\tt MFLOAT} and {\tt void} and their parameters arerestricted to the types {\tt MINT}, {\tt MFLOAT} and {\emstrings}. \index{strings} If the function should run on the \MUSIC\computer then, of course, it must contain data-parallel code.\subsection{Header Files}\index{neuro-functions!header files}%-----------------------------------Each C module which contains neuro-functions has to include thefollowing header files (from the {\tt basic} subdirectory. Thesequence of including is relevant.):\begin{list}{}{\renewcommand{\makelabel}[1]{\tt#1}}\item [allnfcts.h] contains the prototype of all  neuro-functions\item [allnobjs.h] contains the type definitions of the  neuro-objects\item [neurolib.h] contains various definitions which are  used to program neuro-functions.\end{list}\subsection{Register Neuro-Functions}\index{neuro-functions!registration}%------------------------------------To make a function ``visible'' from the NeuroBasic interpreter, theprogrammer has to put an {\small ANSI}-like C prototype of thefunction into the file {\tt nfcts.h} \index{nfcts.h@{\tt nfcts.h}} ofthe local subdirectory of the package it belongs to. The prototype forthe squashing function ({\tt sqtanh()}) of a back-propagation layer,for instance, is\begin{verbatim}  MINT sqtanh(MINT layer, MFLOAT alpha, MFLOAT beta);\end{verbatim}\noindent (The parameters {\em alpha\/} and {\em beta\/} arestretching factors of the squashing function in vertical andhorizontal direction.)Note that string parameters \index{strings} have to be declared asarrays of characters ({\tt char~str[]}) rather than pointers tocharacters ({\tt char~*str}). Otherwise the program {\tt mknfcts} willconfuse strings and open parameter lists (seeSection~\ref{sec_openpar}).The global makefile then calls the utility program {\tt mknfcts} whichreads the {\tt nfcts.h} files of all subdirectories and creates thetwo files {\tt basicif.c} and {\tt allnfcts.h} in the {\tt basic}subdirectory. The first one contains interface code to translate theBasic function call into a C function call and the latter one is alist of all neuro-function prototypes of all packages. It is a goodidea to include this file into any C module which containsneuro-function code because inconsistencies between the functiondefinition and its prototype are then reported by the compiler.\subsection{Variable Number of Parameters}\index{neuro-functions!variable parameter list}\label{sec_openpar}%----------------------------------------------It is possible to write neuro-functions with a variable number ofparameters when called from the Basic interpreter. This means thefunction gets as many parameters as the user types in from the Basicinterpreter. Such an open parameter list will be translated into anarray of floating-point numbers and the number of parameters and apointer to the array are passed to the corresponding C function. Thefollowing example function returns the sum of its arguments.\begin{verbatim}  MFLOAT sum(MINT nargs, MFLOAT *pargs)  {    MINT i;    MFLOAT s;    s = 0;    for (i = 0; i < nargs; i++) s = s + pargs[i];    return s;  }\end{verbatim}Assuming the correct declaration in {\tt nfcts.h} this function can becalled from the Basic interpreter, for example, as follows:\begin{verbatim}  Ok print sum(1, 4, -7, 9)     7\end{verbatim}\noindent It is also possible to have a certain number of fixedparameters in the function. Just the last two arguments must be oftype {\tt MINT} (the number of remaining arguments) and a pointer to afloating-point array, {\tt MFLOAT*}, (the remaining arguments).Don't use the notation {\tt MFLOAT pargs[\thinspace]} in the functionprototype because it will not be recognized by the {\tt mknfcts}program as being a function with an open parameter list.\subsection{Error Messages}\index{neuro-functions!error messages}%-------------------------------------A neuro-function can report errors to the Basic interpreter by usingthe two global variables {\tt fn\_error} and {\ttfn\_error\_msg}. {\tt fn\_error} is an integer containing the errorcode. {\tt fn\_error\_msg} is the pointer to a string containing anerror message which will be displayed on the user screen. Note thatsuch a string must be static, \ie it must exist also after theneuro-function itself has terminated. The macro {\tt ERR\_GERROR}($=~-1$) is used as error code for a general error, although the Basicinterpreter considers everything except 0 as an error code.The Basic interpreter tests the error variable after each call of aneuro-function. If it is nonzero, then it prints out the error messagepointed to by {\tt fn\_error\_msg} and stops the execution of theBasic program.The following error codes have predefined error messages. If they areused, no message needs to be written into {\tt fn\_error\_msg}.\bigskip\begin{tabular}{lrl}{\tt ERR\_NOERROR}        & $ 0$    & (no error)\\{\tt ERR\_OBJTABFULL}     & $-2$    & (object table full)\\{\tt ERR\_NOMEM}          & $-3$    & (not enough memory)\\{\tt ERR\_NOOBJ}          & $-4$    & (not an object)\\{\tt ERR\_WRONGOBJ}       & $-5$    & (wrong object type)\\{\tt ERR\_PARCONF}        & $-6$    & (parameter conflict)\\{\tt ERR\_DISKERROR}      & $-7$    & (disk I/O error)\\{\tt ERR\_NOFILE}         & $-8$    & (file not found)\\\end{tabular}\index{neuro-functions|)}\section{Neuro-Objects}\index{neuro-objects|(}

⌨️ 快捷键说明

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