1 /*****************************************************************************
2 FILE : $Source: /projects/higgs1/SNNS/CVS/SNNS/kernel/sources/kr_funcs.c,v $
3 SHORTNAME :
4 SNNS VERSION : 4.2
5
6 PURPOSE : SNNS-Kernel: Management routines for user functions
7 NOTES :
8
9 AUTHOR : Niels Mache
10 DATE : 12.02.90
11
12 CHANGED BY : Sven Doering
13 RCS VERSION : $Revision: 2.15 $
14 LAST CHANGE : $Date: 1998/03/03 14:08:37 $
15
16 Copyright (c) 1990-1995 SNNS Group, IPVR, Univ. Stuttgart, FRG
17 Copyright (c) 1996-1998 SNNS Group, WSI, Univ. Tuebingen, FRG
18
19 ******************************************************************************/
20 #include <config.h>
21 #include <string.h>
22
23 #include "kr_typ.h" /* Kernel types and constants */
24 #include "kr_const.h" /* Kernel constants */
25 #include "kr_def.h" /* Default values */
26 #include "kr_funcs.ph" /* Function prototypes */
27 #include "kernel.h"
28 #include "func_tbl.h"
29 #include "glob_typ.h"
30
31
32 /*#################################################
33
34 GROUP: MasPar functions
35
36 #################################################*/
37
38 #ifdef MASPAR_KERNEL
39 #ifndef MASPAR_KERNEL_EMULATION
40
41 extern krmp_getMasParFuncInfo();
42
43 #endif
44 #endif
45
46
47 /*#################################################
48
49 GROUP: Functions
50
51 #################################################*/
52 /*****************************************************************************
53 FUNCTION : krf_getInternalFuncInfo
54
55 PURPOSE :
56 NOTES :
57
58 RETURNS :
59 UPDATE :
60 ******************************************************************************/
61 extern FlintType OUT_Custom_Python(FlintType act);
62 extern FlintType ACT_Custom_Python(struct Unit * unit_ptr);
63 extern FlintType ACT_DERIV_Custom_Python(struct Unit * unit_ptr);
64 extern FlintType ACT_2_DERIV_Custom_Python(struct Unit * unit_ptr);
65
krf_getInternalFuncInfo(int mode,struct FuncInfoDescriptor * func_descr)66 static krui_err krf_getInternalFuncInfo(int mode, struct FuncInfoDescriptor *func_descr)
67 {
68 struct FuncTable *ftbl_ptr;
69 unsigned short func_type;
70
71 KernelErrorCode = KRERR_NO_ERROR;
72
73 switch (mode) {
74 case GET_NO_OF_FUNCS:
75 func_descr->number = NoOfKernelFuncs + kr_getNoOfPythonFunctions();
76 break;
77
78 case GET_FUNC_INFO: /* return all info about given function */
79 if ((func_descr->number < 0) || (func_descr->number >=
80 (NoOfKernelFuncs + kr_getNoOfPythonFunctions()))) {
81 KernelErrorCode = KRERR_PARAMETERS;
82 return( KernelErrorCode );
83 }
84
85 if(func_descr->number < NoOfKernelFuncs) {
86 ftbl_ptr = kernel_func_table + func_descr->number;
87
88 strcpy( func_descr->func_name, ftbl_ptr->func_name );
89 func_descr->func_type = ftbl_ptr->func_type & ~DEFAULT_FUNC;
90 func_descr->no_of_input_parameters = ftbl_ptr->no_of_input_parameters;
91 func_descr->no_of_output_parameters = ftbl_ptr->no_of_output_parameters;
92 func_descr->function = ftbl_ptr->function;
93 #ifdef PARAGON_KERNEL
94 func_descr->parallelized = ftbl_ptr->parallelized;
95 #endif
96 } else { /* Must be a Python function */
97 KernelErrorCode = kr_getPythonFuncInfo(mode, func_descr);
98 }
99 break;
100
101 case SEARCH_FUNC: /* search for the given function and return the
102 info about the function */
103
104 for (ftbl_ptr = kernel_func_table;
105 ftbl_ptr < kernel_func_table + NoOfKernelFuncs;
106 ftbl_ptr++) {
107 func_type = func_descr->func_type;
108 if (((func_type & FUNC_TYPE_MASK) == (ftbl_ptr->func_type & FUNC_TYPE_MASK)) &&
109 (strcmp( func_descr->func_name, ftbl_ptr->func_name ) == 0)) {
110 func_descr->no_of_input_parameters = ftbl_ptr->no_of_input_parameters;
111 func_descr->no_of_output_parameters = ftbl_ptr->no_of_output_parameters;
112 func_descr->function = ftbl_ptr->function;
113 #ifdef PARAGON_KERNEL
114 func_descr->parallelized = ftbl_ptr->parallelized;
115 #endif
116 return( KRERR_NO_ERROR );
117 }
118 }
119 KernelErrorCode = kr_getPythonFuncInfo(mode,func_descr);
120 if(!KernelErrorCode) {
121 switch (func_descr->func_type) {
122 case OUT_FUNC:
123 func_descr->function =
124 (FunctionPtr)OUT_Custom_Python;
125 break;
126 case ACT_FUNC:
127 func_descr->function =
128 (FunctionPtr)ACT_Custom_Python;
129 break;
130 case ACT_DERIV_FUNC:
131 func_descr->function =
132 (FunctionPtr)ACT_DERIV_Custom_Python;
133 break;
134 case ACT_2_DERIV_FUNC:
135 func_descr->function =
136 (FunctionPtr)ACT_2_DERIV_Custom_Python;
137 break;
138 default:
139 fputs("Unhandled Python function type\n",
140 stderr);
141 }
142 return KRERR_NO_ERROR;
143 }
144 func_descr->function = NULL;
145 #ifdef PARAGON_KERNEL
146 func_descr->parallelized = ftbl_ptr->parallelized;
147 #endif
148
149 switch (func_descr->func_type) {
150 case OUT_FUNC:
151 KernelErrorCode = KRERR_OUTFUNC;
152 break;
153 case ACT_FUNC:
154 KernelErrorCode = KRERR_ACTFUNC;
155 break;
156 case SITE_FUNC:
157 KernelErrorCode = KRERR_SITEFUNC;
158 break;
159 case LEARN_FUNC:
160 KernelErrorCode = KRERR_LEARNING_FUNC;
161 break;
162 case UPDATE_FUNC:
163 KernelErrorCode = KRERR_UPDATE_FUNC;
164 break;
165 case INIT_FUNC:
166 KernelErrorCode = KRERR_INIT_FUNC;
167 break;
168 case ACT_DERIV_FUNC:
169 case ACT_2_DERIV_FUNC:
170 KernelErrorCode = KRERR_DERIV_FUNC;
171 break;
172 case REMAP_FUNC:
173 KernelErrorCode = KRERR_REMAP_FUNC;
174 break;
175
176 default:
177 KernelErrorCode = KRERR_PARAMETERS;
178 }
179
180 break;
181
182 case GET_FUNC_NAME: /* search for the given function pointer and
183 returns the name of the function */
184
185 for (ftbl_ptr = kernel_func_table;
186 ftbl_ptr < kernel_func_table + NoOfKernelFuncs;
187 ftbl_ptr++)
188 if (func_descr->function == ftbl_ptr->function) {
189 strcpy( func_descr->func_name, ftbl_ptr->func_name );
190 func_descr->func_type = ftbl_ptr->func_type & ~DEFAULT_FUNC;
191 func_descr->no_of_input_parameters = ftbl_ptr->no_of_input_parameters;
192 func_descr->no_of_output_parameters = ftbl_ptr->no_of_output_parameters;
193 #ifdef PARAGON_KERNEL
194 func_descr->parallelized = ftbl_ptr->parallelized;
195 #endif
196
197 return( KRERR_NO_ERROR );
198 }
199 KernelErrorCode = kr_getPythonFuncInfo(GET_FUNC_NAME,func_descr);
200 if(KernelErrorCode == KRERR_NO_ERROR) return KRERR_NO_ERROR;
201 func_descr->func_type = 0;
202 break;
203
204 case GET_DEFAULT_FUNC: /* search for the given function type and
205 returns the default function of this type */
206
207 func_type = (func_descr->func_type | DEFAULT_FUNC) & ~FF_LEARN_FUNC;
208 for (ftbl_ptr = kernel_func_table;
209 ftbl_ptr < kernel_func_table + NoOfKernelFuncs;
210 ftbl_ptr++) {
211 if (func_type == (ftbl_ptr->func_type & ~FF_LEARN_FUNC)) {
212 func_descr->no_of_input_parameters = ftbl_ptr->no_of_input_parameters;
213 func_descr->no_of_output_parameters = ftbl_ptr->no_of_output_parameters;
214 func_descr->function = ftbl_ptr->function;
215 strcpy( func_descr->func_name, ftbl_ptr->func_name );
216 func_descr->number = ftbl_ptr - kernel_func_table;
217 return( KRERR_NO_ERROR );
218 }
219 }
220
221 func_descr->number = 0;
222 KernelErrorCode = KRERR_MISSING_DEFAULT_FUNC;
223 break;
224
225 default:
226 KernelErrorCode = KRERR_PARAMETERS;
227 }
228
229 return( KernelErrorCode );
230 }
231
232
233 /*****************************************************************************
234 FUNCTION : krf_getFuncInfo
235
236 PURPOSE :
237 NOTES :
238
239 RETURNS :
240 UPDATE :
241 ******************************************************************************/
242
krf_getFuncInfo(int mode,struct FuncInfoDescriptor * func_descr)243 krui_err krf_getFuncInfo(int mode, struct FuncInfoDescriptor *func_descr)
244 {
245 switch (specialNetworkType) {
246 case NET_TYPE_GENERAL:
247 (void) krf_getInternalFuncInfo( mode, func_descr );
248 break;
249
250 #ifdef MASPAR_KERNEL
251
252 case NET_TYPE_FF1:
253
254 #ifndef MASPAR_KERNEL_EMULATION
255
256
257 KernelErrorCode = callRequest( krmp_getMasParFuncInfo,
258 sizeof (int) +
259 sizeof (struct FuncInfoDescriptor *),
260 mode, func_descr );
261 #else
262 (void) krf_getInternalFuncInfo( mode, func_descr );
263 #endif
264
265 break;
266 #endif
267
268 default:
269 KernelErrorCode = KRERR_PARAMETERS;
270 }
271
272 return( KernelErrorCode );
273 }
274
275 /*****************************************************************************
276 FUNCTION : krf_getNoOfFuncs
277
278 PURPOSE : Returns the number of functions in the function table
279 NOTES :
280
281 RETURNS : Returns the number of functions in the function table
282 UPDATE :
283 ******************************************************************************/
krf_getNoOfFuncs(void)284 int krf_getNoOfFuncs(void)
285 {
286 struct FuncInfoDescriptor functionDescr;
287
288
289 KernelErrorCode = krf_getFuncInfo( GET_NO_OF_FUNCS, &functionDescr );
290 return( functionDescr.number );
291 }
292
293 /*****************************************************************************
294 FUNCTION : krf_funcSearch
295
296 PURPOSE : seaches for the given function (name and type) and returns a pointer to
297 this function. krf_funcSearch(...)
298 NOTES :
299
300 RETURNS : TRUE if the given function was found, FALSE otherwise.
301 UPDATE :
302 ******************************************************************************/
krf_funcSearch(char * func_name,int func_type,FunctionPtr * func_ptr)303 bool krf_funcSearch(char *func_name, int func_type, FunctionPtr *func_ptr)
304 {
305 struct FuncInfoDescriptor functionDescr;
306
307 functionDescr.func_type = func_type;
308 strcpy( functionDescr.func_name, func_name );
309
310 KernelErrorCode = krf_getFuncInfo( SEARCH_FUNC, &functionDescr );
311
312 if (KernelErrorCode != KRERR_NO_ERROR) return( FALSE );
313 *func_ptr = functionDescr.function;
314 return( TRUE );
315 }
316 /*****************************************************************************
317 FUNCTION : krf_getFuncName
318
319 PURPOSE :
320 NOTES :
321
322 RETURNS : Returns the name of the given function
323 UPDATE :
324 ******************************************************************************/
krf_getFuncName(FunctionPtr func_ptr)325 char *krf_getFuncName(FunctionPtr func_ptr)
326 {
327 static struct FuncInfoDescriptor functionDescr;
328
329 functionDescr.function = func_ptr;
330
331 KernelErrorCode = krf_getFuncInfo( GET_FUNC_NAME, &functionDescr );
332
333 if (functionDescr.func_type == 0) return( NULL );
334
335 return( functionDescr.func_name );
336 }
337
338
339 /*#################################################
340
341 GROUP: Functions for managing current and
342 default network functions
343
344 #################################################*/
345 /*****************************************************************************
346 FUNCTION : krf_getCurrentNetworkFunc
347
348 PURPOSE :
349 NOTES :
350
351 RETURNS : returns the name of the current network function
352 UPDATE :
353 ******************************************************************************/
krf_getCurrentNetworkFunc(int type)354 char *krf_getCurrentNetworkFunc(int type)
355 {
356 int sel;
357 static struct FuncInfoDescriptor func_descr;
358
359
360 KernelErrorCode = KRERR_NO_ERROR;
361
362 if (specialNetworkType == NET_TYPE_GENERAL) sel = 0;
363 else sel = NO_OF_FUNC_TYPES;
364
365 switch (type) {
366 case UPDATE_FUNC:
367 break;
368 case LEARN_FUNC:
369 sel += 1;
370 break;
371 case INIT_FUNC:
372 sel += 2;
373 break;
374 case OUT_FUNC:
375 sel += 3;
376 break;
377 case ACT_FUNC:
378 sel += 4;
379 break;
380 case SITE_FUNC:
381 sel += 5;
382 break;
383 case UPDATE_FUNC | BENCH_FUNC:
384 sel += 6;
385 break;
386 case LEARN_FUNC | BENCH_FUNC:
387 sel += 7;
388 break;
389 case LEARN_FUNC | FF_LEARN_FUNC:
390 sel += 8;
391 break;
392 case PRUNING_FUNC:
393 sel += 9;
394 break;
395 case TEST_FUNC:
396 sel += 10;
397 break;
398 default:
399 KernelErrorCode = KRERR_PARAMETERS;
400 return( NULL );
401 }
402
403 if (netFuncInit[sel]) return( &CurrNetworkFunc[sel][0] );
404
405 func_descr.func_type = type;
406 if (krf_getFuncInfo( GET_DEFAULT_FUNC, &func_descr ) == KRERR_NO_ERROR)
407 return( func_descr.func_name );
408
409 return( NULL );
410 }
411
412 /*****************************************************************************
413 FUNCTION : krf_storeCurrentNetworkFunc
414
415 PURPOSE :
416 NOTES :
417
418 RETURNS :
419 UPDATE :
420 ******************************************************************************/
421
krf_storeCurrentNetworkFunc(char * function_name,int type)422 static void krf_storeCurrentNetworkFunc(char *function_name, int type)
423 {
424 int sel;
425
426 if (specialNetworkType == NET_TYPE_GENERAL) sel = 0;
427 else sel = 3;
428
429
430 switch (type) {
431 case UPDATE_FUNC:
432 break;
433 case LEARN_FUNC:
434 sel += 1;
435 break;
436 case INIT_FUNC:
437 sel += 2;
438 break;
439 case OUT_FUNC:
440 sel += 3;
441 break;
442 case ACT_FUNC:
443 sel += 4;
444 break;
445 case SITE_FUNC:
446 sel += 5;
447 break;
448 case UPDATE_FUNC | BENCH_FUNC:
449 sel += 6;
450 break;
451 case LEARN_FUNC | BENCH_FUNC:
452 sel += 7;
453 break;
454 case LEARN_FUNC | FF_LEARN_FUNC:
455 sel += 8;
456 break;
457 case PRUNING_FUNC:
458 sel += 9;
459 break;
460 case TEST_FUNC:
461 sel += 10;
462 break;
463 default:
464 KernelErrorCode = KRERR_PARAMETERS;
465 return;
466 }
467
468 netFuncInit[sel] = TRUE;
469 strcpy( &CurrNetworkFunc[sel][0], function_name );
470 return;
471 }
472
473
474
475 /*****************************************************************************
476 FUNCTION : krf_setCurrentNetworkFunc
477
478 PURPOSE : sets the current network function.
479 NOTES :
480
481 RETURNS :
482 UPDATE :
483 ******************************************************************************/
krf_setCurrentNetworkFunc(char * function_name,int type)484 krui_err krf_setCurrentNetworkFunc(char *function_name, int type)
485 {
486 FunctionPtr func_ptr;
487 bool func_has_changed;
488 char *current_func;
489
490 KernelErrorCode = KRERR_NO_ERROR;
491
492 if (!krf_funcSearch( function_name, type, &func_ptr ))
493 return( KernelErrorCode );
494 if ((current_func = krf_getCurrentNetworkFunc( type )) == NULL)
495 return( KernelErrorCode );
496
497 func_has_changed = strcmp( function_name, current_func ) != 0;
498
499 switch (type) {
500 case UPDATE_FUNC:
501 if (func_has_changed)
502 { /* update function has changed, initialize the network */
503 NetInitialize = TRUE;
504 krf_storeCurrentNetworkFunc( function_name, type );
505 }
506
507 return( KernelErrorCode );
508
509 case LEARN_FUNC:
510 if (func_has_changed)
511 { /* Learning function has changed */
512 LearnFuncHasChanged = TRUE;
513 krf_storeCurrentNetworkFunc( function_name, type );
514 krf_storeCurrentNetworkFunc( function_name, TEST_FUNC );
515 }
516
517 return( KernelErrorCode );
518
519 case PRUNING_FUNC:
520 if (func_has_changed)
521 krf_storeCurrentNetworkFunc (function_name, type);
522 break;
523
524 case (FF_LEARN_FUNC | LEARN_FUNC):
525 if (func_has_changed)
526 krf_storeCurrentNetworkFunc (function_name, type);
527 krf_storeCurrentNetworkFunc( function_name, TEST_FUNC );
528 break;
529
530 case INIT_FUNC:
531 case OUT_FUNC:
532 case ACT_FUNC:
533 case SITE_FUNC:
534 krf_storeCurrentNetworkFunc( function_name, type );
535 break;
536
537 default:
538 KernelErrorCode = KRERR_PARAMETERS;
539 return( KernelErrorCode );
540 }
541
542 return( KernelErrorCode );
543 }
544
545
546
547
548