Annotation of OpenXM/src/ox_gsl/ox_gsl.c, Revision 1.2
1.2 ! takayama 1: /* $OpenXM: OpenXM/src/ox_gsl/ox_gsl.c,v 1.1 2018/03/29 05:47:11 takayama Exp $
1.1 takayama 2: */
3:
4: #include <stdio.h>
5: #include <stdlib.h>
1.2 ! takayama 6: #include <setjmp.h>
! 7: #include <string.h>
1.1 takayama 8: #include "ox_toolkit.h"
9:
10: OXFILE *fd_rw;
11:
12: #define INIT_S_SIZE 2048
13: #define EXT_S_SIZE 2048
14:
15: static int stack_size = 0;
16: static int stack_pointer = 0;
17: static cmo **stack = NULL;
18:
19: int Debug=1;
20:
1.2 ! takayama 21: void show_stack_top() {
1.1 takayama 22: cmo *data;
23: if (stack_pointer > 0) {
24: data=stack[stack_pointer-1];
1.2 ! takayama 25: print_cmo(data); printf("\n");
1.1 takayama 26: }else {
27: printf("The stack is empty.\n");
28: }
29: }
30:
31: void init_gc()
32: {
33: GC_INIT();
34: }
35:
1.2 ! takayama 36: void initialize_stack()
1.1 takayama 37: {
38: stack_pointer = 0;
39: stack_size = INIT_S_SIZE;
40: stack = malloc(stack_size*sizeof(cmo*));
41: }
42:
1.2 ! takayama 43: static void extend_stack()
1.1 takayama 44: {
45: int size2 = stack_size + EXT_S_SIZE;
46: cmo **stack2 = malloc(size2*sizeof(cmo*));
47: memcpy(stack2, stack, stack_size*sizeof(cmo *));
48: free(stack);
49: stack = stack2;
50: stack_size = size2;
51: }
52:
1.2 ! takayama 53: void push(cmo* m)
1.1 takayama 54: {
55: stack[stack_pointer] = m;
56: stack_pointer++;
57: if (stack_pointer >= stack_size) {
58: extend_stack();
59: }
60: }
61:
62: cmo* pop()
63: {
64: if (stack_pointer > 0) {
65: stack_pointer--;
66: return stack[stack_pointer];
67: }
68: return new_cmo_null();
69: }
70:
71: void pops(int n)
72: {
73: stack_pointer -= n;
74: if (stack_pointer < 0) {
75: stack_pointer = 0;
76: }
77: }
78:
79: #define OX_GSL_VERSION 2018032901
80: #define ID_STRING "2018/03/29 13:56:00"
81:
82: int sm_mathcap()
83: {
1.2 ! takayama 84: int available_cmo[]={
! 85: CMO_NULL,
! 86: CMO_INT32,
! 87: // CMO_DATUM,
! 88: CMO_STRING,
! 89: CMO_MATHCAP,
! 90: CMO_LIST,
! 91: // CMO_MONOMIAL32,
! 92: CMO_ZZ,
! 93: // CMO_QQ,
! 94: CMO_BIGFLOAT32,
! 95: CMO_COMPLEX,
! 96: CMO_IEEE_DOUBLE_FLOAT,
! 97: CMO_ZERO,
! 98: // CMO_DMS_GENERIC,
! 99: // CMO_RING_BY_NAME,
! 100: // CMO_INDETERMINATE,
! 101: // CMO_DISTRIBUTED_POLYNOMIAL,
! 102: // CMO_RECURSIVE_POLYNOMIAL,
! 103: // CMO_POLYNOMIAL_IN_ONE_VARIABLE,
! 104: CMO_ERROR2,
! 105: 0};
! 106: int available_sm_command[]={
! 107: SM_popCMO,
! 108: SM_popString,
! 109: SM_mathcap,
! 110: SM_pops,
! 111: // SM_executeStringByLocalParser,
! 112: SM_executeFunction,
! 113: SM_setMathCap,
! 114: SM_shutdown,
! 115: SM_control_kill,
! 116: SM_control_reset_connection,
! 117: SM_control_spawn_server,
! 118: SM_control_terminate_server,
! 119: 0};
! 120: mathcap_init(OX_GSL_VERSION, ID_STRING, "ox_gsl", available_cmo,available_sm_command);
! 121: push((cmo *)oxf_cmo_mathcap(fd_rw));
1.1 takayama 122: return 0;
123: }
124:
125: int sm_popCMO()
126: {
127: cmo* m = pop();
128:
129: if (m != NULL) {
130: send_ox_cmo(fd_rw, m);
131: return 0;
132: }
133: return SM_popCMO;
134: }
135:
1.2 ! takayama 136: cmo *make_error2(int code)
1.1 takayama 137: {
1.2 ! takayama 138: fprintf(stderr,"make_error2: not implemented.\n");
! 139: return ((cmo *)new_cmo_int32(-1));
1.1 takayama 140: }
141:
142: int get_i()
143: {
144: cmo *c = pop();
145: if (c->tag == CMO_INT32) {
146: return ((cmo_int32 *)c)->i;
147: }else if (c->tag == CMO_ZZ) {
148: return mpz_get_si(((cmo_zz *)c)->mpz);
1.2 ! takayama 149: }else if (c->tag == CMO_NULL) {
! 150: return(0);
! 151: }else if (c->tag == CMO_ZERO) {
! 152: return(0);
1.1 takayama 153: }
154: make_error2(-1);
155: return 0;
156: }
157:
1.2 ! takayama 158: void get_xy(int *x, int *y)
1.1 takayama 159: {
160: pop();
161: *x = get_i();
162: *y = get_i();
163: }
164:
1.2 ! takayama 165: void my_add_int32()
1.1 takayama 166: {
167: int x, y;
168: get_xy(&x, &y);
1.2 ! takayama 169: push((cmo *)new_cmo_int32(x+y));
1.1 takayama 170: }
171:
1.2 ! takayama 172: double get_double()
! 173: {
! 174: cmo *c = pop();
! 175: if (c->tag == CMO_INT32) {
! 176: return( (double) (((cmo_int32 *)c)->i) );
! 177: }else if (c->tag == CMO_IEEE_DOUBLE_FLOAT) {
! 178: return ((cmo_double *)c)->d; // see ox_toolkit.h
! 179: }else if (c->tag == CMO_ZZ) {
! 180: return( (double) mpz_get_si(((cmo_zz *)c)->mpz));
! 181: }else if (c->tag == CMO_NULL) {
! 182: return(0);
! 183: }else if (c->tag == CMO_ZERO) {
! 184: return(0);
! 185: }
! 186: make_error2(-1);
! 187: return 0;
! 188: }
! 189:
! 190: void my_add_double() {
! 191: double x,y;
! 192: pop();
! 193: y = get_double();
! 194: x = get_double();
! 195: push((cmo *)new_cmo_double(x+y));
! 196: }
! 197:
! 198: double *get_double_list(int *length) {
! 199: cmo *c;
! 200: cmo *entry;
! 201: cell *cellp;
! 202: double *d;
! 203: int n,i;
! 204: c = pop();
! 205: if (c->tag != CMO_LIST) {
! 206: make_error2(-1);
! 207: *length=-1; return(0);
! 208: }
! 209: n = *length = list_length((cmo_list *)c);
! 210: d = (double *) GC_malloc(sizeof(double)*(*length+1));
! 211: cellp = list_first((cmo_list *)c);
! 212: entry = cellp->cmo;
! 213: for (i=0; i<n; i++) {
! 214: if (Debug) {
! 215: printf("entry[%d]=",i); print_cmo(entry); printf("\n");
! 216: }
! 217: if (entry->tag == CMO_INT32) {
! 218: d[i]=( (double) (((cmo_int32 *)entry)->i) );
! 219: }else if (entry->tag == CMO_IEEE_DOUBLE_FLOAT) {
! 220: d[i]=((cmo_double *)entry)->d;
! 221: }else if (entry->tag == CMO_ZZ) {
! 222: d[i]=( (double) mpz_get_si(((cmo_zz *)entry)->mpz));
! 223: }else if (entry->tag == CMO_NULL) {
! 224: d[i]= 0;
! 225: }else {
! 226: fprintf(stderr,"entries of the list should be int32 or zz or double\n");
! 227: make_error2(-1);
! 228: *length = -1;
! 229: return(NULL);
! 230: }
! 231: cellp = list_next(cellp);
! 232: entry = cellp->cmo;
! 233: }
! 234: return(d);
! 235: }
! 236: void show_double_list() {
! 237: int n;
! 238: double *d;
! 239: int i;
! 240: pop(); // pop argument number;
! 241: d = get_double_list(&n);
! 242: printf("show_double_list: length=%d\n",n);
! 243: for (i=0; i<n; i++) {
! 244: printf("%lg, ",d[i]);
! 245: }
! 246: printf("\n");
! 247: }
! 248:
! 249: char *get_string() {
! 250: cmo *c;
! 251: c = pop();
! 252: if (c->tag == CMO_STRING) {
! 253: return (((cmo_string *)c)->s);
! 254: }
! 255: make_error2(-1);
! 256: return(NULL);
! 257: }
1.1 takayama 258:
259: int sm_executeFunction()
260: {
261: cmo_string *func = (cmo_string *)pop();
262: if (func->tag != CMO_STRING) {
263: push(make_error2(0));
264: return -1;
265: }
1.2 ! takayama 266: // Test functions
1.1 takayama 267: if (strcmp(func->s, "add_int32") == 0) {
268: my_add_int32();
1.2 ! takayama 269: }else if (strcmp(func->s,"add_double")==0) {
! 270: my_add_double();
! 271: }else if (strcmp(func->s,"show_double_list")==0) {
! 272: show_double_list();
! 273: // The following functions are defined in call_gsl.c
! 274: }else if (strcmp(func->s,"gsl_sf_lngamma_complex_e")==0) {
! 275: call_gsl_sf_lngamma_complex_e();
1.1 takayama 276: }else {
277: push(make_error2(0));
278: return -1;
1.2 ! takayama 279: }
! 280: return(0);
1.1 takayama 281: }
282:
283:
284: int receive_and_execute_sm_command()
285: {
286: int code = receive_int32(fd_rw);
287: switch(code) {
288: case SM_popCMO:
289: sm_popCMO();
290: break;
291: case SM_executeFunction:
292: sm_executeFunction();
293: break;
294: case SM_mathcap:
295: sm_mathcap();
296: break;
297: case SM_setMathCap:
298: pop();
299: break;
300: default:
301: ;
302: }
1.2 ! takayama 303: return(0);
1.1 takayama 304: }
305:
306: int receive()
307: {
308: int tag;
309:
310: tag = receive_ox_tag(fd_rw);
311: switch(tag) {
312: case OX_DATA:
313: push(receive_cmo(fd_rw));
314: if (Debug) show_stack_top();
315: break;
316: case OX_COMMAND:
317: if (Debug) show_stack_top();
318: receive_and_execute_sm_command();
319: break;
320: default:
321: ;
322: }
323: return 0;
324: }
325:
1.2 ! takayama 326: jmp_buf Ox_env;
! 327:
! 328: void usr1_handler(int sig)
! 329: {
! 330: longjmp(Ox_env,1);
! 331: }
! 332:
1.1 takayama 333: int main()
334: {
1.2 ! takayama 335: if ( setjmp(Ox_env) ) {
! 336: fprintf(stderr,"resetting libgsl and sending OX_SYNC_BALL...");
! 337: initialize_stack();
! 338: send_ox_tag(fd_rw,OX_SYNC_BALL);
! 339: fprintf(stderr,"done\n");
! 340: }else{
1.1 takayama 341: ox_stderr_init(stderr);
342: initialize_stack();
343: init_gc();
344: fd_rw = oxf_open(3);
345: oxf_determine_byteorder_server(fd_rw);
1.2 ! takayama 346: }
! 347: signal(SIGUSR1,usr1_handler);
! 348:
! 349: while(1) {
! 350: receive();
! 351: }
! 352: return(0);
1.1 takayama 353: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>