📄 host.c
字号:
/****************************************************************//* *//* Name: host.c *//* *//* Project: NeuroBasic, host part *//* *//* Survey: Contains the command interpreter, running on *//* the host processor (or computer) of MUSIC. *//* *//* Author: Urs Mueller *//* Electronics Laboratory, ETH Zuerich *//* Switzerland *//* *//* Created: August 13, 1992 *//* Modified: December 6, 1994 (um) *//* *//****************************************************************/#include <stdio.h>#include <stdlib.h>#include <math.h>#include <string.h>#include <setjmp.h>#include <m_host.h>#include "basic.h"#include "host.h"#include "allhfcts.h"#define BUF_SIZE 16384L /* used for downloading */ /* patterns */void prg_version(void)/********************/{#ifndef __EXTENDED_COPYRIGHT__ printf(INDENT); printf("Welcome to Neuro-Basic %s. Last compilation: %s\n", VERSION, compilation_date);#else printf(" =========================================================================\n"); printf(" | N E U R O B A S I C %s |\n", VERSION); printf(" =========================================================================\n\n"); printf(" by Urs A. Mueller\n"); printf(" compiled: %s\n\n", compilation_date); printf(" This PC-version of NeuroBasic, the flexible neural net simulator,\n"); printf(" is freeware and can be copied.\n\n\n"); printf(" For more information please contact:\n\n"); printf(" Supercomputing Systems AG\n"); printf(" Pfingstweidstrasse 30\n"); printf(" CH-8005 Zurich, Switzerland\n\n"); printf(" Tel.: ++41-1-445-1600\n"); printf(" Fax.: ++41-1-445-1610\n"); printf(" e-mail: 100316.2330@compuserve.com\n\n\n\n");#endif} /* end of prg_version() */static void mem_move(char *dest, char *src, size_t len)/*****************************************************//* like memmove() of ANSI-C. It's programmed here because I couldn't find this function in the Sun OS library (um)*/{ size_t i; if (dest < src) { for (i = 0; i < len; i++) *dest++ = *src++; } else if (dest > src) { dest += len; src += len; for (i = 0; i < len; i++) *--dest = *--src; }} /* end of mem_move() */static void align_cmatrix(char *pb, MINT rsb, MINT rsw, MINT nr)/*============================================================*//* pb = block pointer rsb = row size in byte rsw = row size in word nr = number of rows to be aligned*/{ char *ps, *pd; long i; size_t rest; rest = rsw * 4 - rsb; ps = pb + (nr-1) * rsb; pd = pb + (nr-1) * rsw * 4; for (i = 1; i < nr; i++) /* first row dosn't need alignment */ { mem_move(pd, ps, rsb); /* align one row */ memset(pd+rsb, 0, rest); /* zero unused bytes */ ps -= rsb; pd -= rsw * 4; }} /* end of align_cmatrix() */static void float2fixed(MFLOAT *pbuf, MINT block_size)/*==================================================*/{ MFLOAT *psrc, x; signed char *pdest; MINT i; psrc = pbuf; pdest = (signed char*) pbuf; for (i = 0; i < block_size; i++) { x = *psrc++; if (x > 8.0 - 1.0/16.0) *pdest++ = 0x7f; else if (x < -8.0) *pdest++ = 0x80; else *pdest++ = (signed char)(x * 16); }} /* end of float2fixed() */static void fixed2float(MFLOAT *pbuf, MINT block_size)/*==================================================*/{ signed char *psrc; MFLOAT *pdest; MINT i; psrc = (signed char*)pbuf + block_size; pdest = pbuf + block_size; for (i = 0; i < block_size; i++) { *--pdest = (MFLOAT)(*--psrc) / 16.0; }} /* end of fixed2float() */static MINT load_block(MFLOAT *pbuf, MINT fformat, MINT block_size, FILE *fpt)/*===============================================================*/{ MINT n; if (fformat == FF_FLOAT32) { n = fread(pbuf, sizeof(MFLOAT), block_size, fpt); } else if (fformat == FF_FIXED8) { n = fread(pbuf, sizeof(char), block_size, fpt); } else /* FF_ASCII */ { for (n = 0; n < block_size && fscanf(fpt, "%f", pbuf++) == 1; n++); } return (n == block_size);} /* end of load_block() */void basic_load(message_t message)/********************************//* Opens the SN-matrix file "filename" (can be floating-point, fixed point or ASCII) and down-loads the matrix to MUSIC in the format given by "lformat" using blocks of "patterns_per_block" patterns. If necessary, the numbers are converted. The dimensions of the matrix are checked against "ndim", "xdim", "ydim" and "zdim". If a matrix is sent to MUSIC in fixed-point format (1 byte per value) then the y-dimension is rounded up to a full number of Music words (4 bytes). This is only used for pattern files.*/{ MINT lformat, block_size, ndim, xdim, ydim, zdim; FILE *fpt; char *filename; sn_mathead_t sn_header; message_t msg; MINT success = TRUE, fformat, i, ndots, nnewdots; MINT ntotal_values, values_copied, current_block_size; MINT ydim_word, ncurrent_rows; MFLOAT *pbuf; char *msgs, amagic[5]; filename = bc_str_get(message[2].i, -1, NULL); Rd_from_music(msg, sizeof(msg)); ndim = msg[0].i; xdim = msg[1].i; ydim = msg[2].i; zdim = msg[3].i; block_size = msg[4].i; lformat = msg[5].i; /*===== check SN matrix file =====*/ memset((char*)(&sn_header), 0, sizeof(sn_header)); fpt = fopen(filename, "rb"); if (fpt == NULL) { msg[0].i = ERR_NOFILE; Wr_to_music(msg, sizeof(msg)); /* tell Music */ return; } fread(&sn_header, sizeof(sn_header), 1, fpt); if (sn_header.sn_magic == SN_FMAGIC) { fformat = FF_FLOAT32; msgs = "floating-point"; } else if (sn_header.sn_magic == SN_CMAGIC) { fformat = FF_FIXED8; msgs = "fixed-point"; } else { freopen(filename, "r", fpt); /* reopen as text file */ fscanf(fpt, "%c%c%c%c", amagic, amagic+1, amagic+2, amagic+3); amagic[4] = 0; if (strcmp(amagic, SN_AMAGIC) == 0) { sn_header.sn_ydim = sn_header.sn_zdim = 1; fscanf(fpt, "%ld", &sn_header.sn_ndim); if (sn_header.sn_ndim > 0) fscanf(fpt, "%ld", &sn_header.sn_xdim); if (sn_header.sn_ndim > 1) fscanf(fpt, "%ld", &sn_header.sn_ydim); if (sn_header.sn_ndim > 2) fscanf(fpt, "%ld", &sn_header.sn_zdim); fformat = FF_ASCII; msgs = "ASCII"; } else { printf(INDENT); printf("Unknown file type of file \"%s\".\n", filename); msg[0].i = ERR_PARCONF; Wr_to_music(msg, sizeof(msg)); /* tell Music */ fclose(fpt); return; } } if (sn_header.sn_ndim != ndim || sn_header.sn_xdim != xdim || sn_header.sn_ydim != ydim || sn_header.sn_zdim != zdim) { printf(INDENT); printf("Matrix file \"%s\" has unexpected dimensions:\n", filename); printf(INDENT); printf("Dimensions: ndim = %ld, %ldx%ldx%ld\n", sn_header.sn_ndim, sn_header.sn_xdim, sn_header.sn_ydim, sn_header.sn_zdim); printf(INDENT); printf("Expected: ndim = %ld, %ldx%ldx%ld\n", ndim, xdim, ydim, zdim); msg[0].i = ERR_PARCONF; Wr_to_music(msg, sizeof(msg)); /* tell Music */ fclose(fpt); return; } pbuf = (MFLOAT*)malloc(BUF_SIZE * sizeof(MFLOAT)); if (pbuf == NULL) { printf(INDENT); printf("Not enough memory on host to load a matrix.\n"); msg[0].i = ERR_NOMEM; Wr_to_music(msg, sizeof(msg)); /* tell Music */ fclose(fpt); return; } msg[0].i = ERR_NOERROR; Wr_to_music(msg, sizeof(msg)); /* tell Music ok */ /*===== down-load matrix =====*/ printf(INDENT); printf("Loading %s matrix \"%s\" of dimensions %ldx%ldx%ld\n", msgs, filename, xdim, ydim, zdim); printf(INDENT); printf("in 10 blocks "); fflush(stdout); ntotal_values = xdim * ydim * zdim; values_copied = 0; ndots = 0; success = TRUE; while (values_copied < ntotal_values) { if (BUF_SIZE > block_size - values_copied % block_size) current_block_size = block_size - values_copied % block_size; else current_block_size = BUF_SIZE; if (current_block_size > ntotal_values - values_copied) current_block_size = ntotal_values - values_copied; if (lformat == FF_FIXED8) { current_block_size -= current_block_size % ydim; if (success) success = load_block(pbuf, fformat, current_block_size, fpt); if (fformat != FF_FIXED8) float2fixed(pbuf, current_block_size); ydim_word = (ydim + 3) / 4; ncurrent_rows = current_block_size / ydim; align_cmatrix((char*)pbuf, ydim, ydim_word, ncurrent_rows); Wr_to_music(pbuf, ncurrent_rows * ydim_word * sizeof(MINT)); } else /* FF_FLOAT32 */ { if (success) success = load_block(pbuf, fformat, current_block_size, fpt); if (fformat == FF_FIXED8) fixed2float(pbuf, current_block_size); Wr_to_music(pbuf, current_block_size * sizeof(MFLOAT)); } values_copied += current_block_size; nnewdots = values_copied * 10 / ntotal_values - ndots; for (i = 0; i < nnewdots; i++) printf("."); fflush(stdout); ndots += nnewdots; } /* end while () */ printf("\n"); fflush(stdout); if (!success) { printf(INDENT); printf("Error while reading file \"%s\".\n", filename); printf(INDENT); printf("The matrix may not be valid!\n"); } free(pbuf);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -