00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <string>
00015 using namespace std;
00016
00017 #include <oct.h>
00018 #include "grpc.h"
00019 #include "gs_oct.h"
00020
00021
00022 int verify_allocation(void *ptr) {
00023 if (ptr == NULL) {
00024 perror("malloc");
00025 return -1;
00026 }
00027
00028 return 0;
00029 }
00030
00031
00032 int is_sparse_index_or_pointer(gs_argument_t *argp, gs_problem_t *pd) {
00033 int is_index_or_pointer = 0;
00034 gs_argument_t* arg_attr;
00035
00036 for (arg_attr = pd->arglist; arg_attr != NULL; arg_attr = arg_attr->next)
00037 if ((arg_attr->objecttype == GS_SPARSEMATRIX) &&
00038 (!strcmp(argp->name, arg_attr->sparse_attr.indices) ||
00039 !strcmp(argp->name, arg_attr->sparse_attr.pointer))) {
00040 is_index_or_pointer = 1;
00041 break;
00042 }
00043
00044 return is_index_or_pointer;
00045 }
00046
00047
00048 int process_gs_out_arguments(gs_problem_t* pd) {
00049 gs_argument_t *argp;
00050 gs_argument_t *arg_indices;
00051 gs_argument_t *arg_pointer;
00052 void *v;
00053 int nrows, ncolumns;
00054 char buf[256];
00055 int fd;
00056 int nfiles;
00057 char **fnames;
00058
00059
00060 for (argp = pd->arglist; argp != NULL; argp = argp->next) {
00061 if (argp->inout != GS_OUT && argp->inout != GS_VAROUT) continue;
00062 if (is_sparse_index_or_pointer(argp, pd)) continue;
00063
00064 switch (argp->objecttype) {
00065 case GS_MATRIX:
00066 if (argp->inout == GS_OUT) {
00067 nrows = argp->rows;
00068 ncolumns = argp->cols;
00069 argp->data = (void *) malloc(gs_datatype_sizeof(
00070 argp->datatype) * nrows * ncolumns);
00071 if (verify_allocation(argp->data) < 0) return -1;
00072 }
00073 else if (argp->inout == GS_VAROUT) {
00074 argp->data = (char **) malloc(sizeof(char *));
00075 }
00076 break;
00077 case GS_SPARSEMATRIX:
00078 if (argp->inout == GS_VAROUT) {
00079 fprintf(stderr, "VAROUT SPARSEMATRIX arguments are not supported\n");
00080 return -1;
00081 }
00082 arg_indices = gs_arg_lookup_by_name(argp->sparse_attr.indices, pd->arglist);
00083 arg_pointer = gs_arg_lookup_by_name(argp->sparse_attr.pointer, pd->arglist);
00084 if (arg_indices == NULL || arg_pointer == NULL) return -1;
00085 arg_indices->data = malloc(sizeof(int) * argp->sparse_attr.nnz);
00086 if (verify_allocation(arg_indices->data) < 0) return -1;
00087 arg_pointer->data = malloc(sizeof(int) *
00088 (argp->sparse_attr.cols_val_saved + 1));
00089 if (verify_allocation(arg_pointer->data) < 0) return -1;
00090 argp->data = (void *) malloc(argp->sparse_attr.nnz *
00091 gs_datatype_sizeof(argp->datatype));
00092 if (verify_allocation(argp->data) < 0) return -1;
00093 break;
00094 case GS_VECTOR:
00095 if (argp->inout == GS_OUT) {
00096 nrows = argp->rows;
00097 argp->data = (void *) malloc(gs_datatype_sizeof(argp->datatype) * nrows);
00098 if (verify_allocation(argp->data) < 0) return -1;
00099 }
00100 else if (argp->inout == GS_VAROUT) {
00101 argp->data = (char **) malloc(sizeof(char *));
00102 }
00103 break;
00104 case GS_SCALAR:
00105 if (argp->inout == GS_OUT) {
00106 argp->data = &(argp->scalar_val);
00107 }
00108 else if (argp->inout == GS_VAROUT) {
00109 fprintf(stderr, "VAROUT SCALAR arguments are not supported\n");
00110 return -1;
00111 }
00112 break;
00113 case GS_FILE:
00114 sprintf(buf, "%s_%s_OUT", pd->name, argp->name);
00115 fd = open(buf, O_WRONLY | O_CREAT | O_TRUNC, 0600);
00116 if(fd < 0) {
00117 fprintf(stderr, "Could not open file '%s'\n", buf);
00118 return -1;
00119 }
00120 close(fd);
00121 argp->data = strdup(buf);
00122 if (argp->data == NULL) return -1;
00123 break;
00124 case GS_PACKEDFILE:
00125 nfiles = argp->rows * argp->cols;
00126 fnames = (char **) malloc(sizeof(char *) * nfiles);
00127 for (int i = 0; i < nfiles; i++) {
00128 sprintf(buf, "%s_%s_%d_OUT", pd->name, argp->name, i + 1);
00129 fd = open(buf, O_WRONLY | O_CREAT | O_TRUNC, 0600);
00130 if(fd < 0) {
00131 fprintf(stderr, "Could not open file '%s'\n", buf);
00132 return -1;
00133 }
00134 close(fd);
00135 fnames[i] = strdup(buf);
00136 if (fnames[i] == NULL) return -1;
00137 }
00138 argp->data = fnames;
00139 break;
00140 default:
00141 break;
00142 }
00143 }
00144 }
00145
00146
00147 int convert_octave_arguments(gs_problem_t* pd, octave_value_list oct_args) {
00148 gs_argument_t *argp;
00149
00150 int i = 1;
00151
00152 for (argp = pd->arglist; argp != NULL; argp = argp->next) {
00153
00154 if (argp->inout == GS_OUT ||
00155 argp->inout == GS_VAROUT ||
00156 argp->inout == GS_WORKSPACE) continue;
00157 if (is_sparse_index_or_pointer(argp, pd)) continue;
00158
00159 if (argp->objecttype == GS_SCALAR) {
00160 if (convert_scalar(argp, oct_args, i) < 0) return -1;
00161 }
00162 i++;
00163 }
00164
00165
00166 if (gs_receiver_compute_arg_sizes(pd, GS_IN) < 0) {
00167 fprintf(stderr, "Failure to compute argument sizes\n");
00168 return -1;
00169 }
00170
00171 i = 1;
00172
00173 for (argp = pd->arglist; argp != NULL; argp = argp->next) {
00174 if (argp->inout == GS_OUT ||
00175 argp->inout == GS_VAROUT ||
00176 argp->inout == GS_WORKSPACE) continue;
00177
00178 if (is_sparse_index_or_pointer(argp, pd)) {
00179 continue;
00180 }
00181
00182 switch (argp->objecttype) {
00183 case GS_MATRIX:
00184 if (convert_matrix(argp, oct_args, i) < 0) return -1;
00185 break;
00186 case GS_SPARSEMATRIX:
00187 if (convert_sparse_matrix(pd, argp, oct_args, i) < 0) return -1;
00188 break;
00189 case GS_VECTOR:
00190 if (convert_vector(argp, oct_args, i) < 0) return -1;
00191 break;
00192 case GS_SCALAR:
00193
00194 break;
00195 case GS_FILE:
00196 if (convert_file_argument(argp, oct_args, i) < 0) return -1;
00197 break;
00198 case GS_PACKEDFILE:
00199 if (convert_packed_file_argument(argp, oct_args, i) < 0) return -1;
00200 break;
00201 default:
00202 break;
00203 }
00204
00205 i++;
00206 }
00207
00208
00209 process_gs_out_arguments(pd);
00210 }
00211
00212
00213 int convert_matrix(gs_argument_t *argp, octave_value_list octval, int index) {
00214 int nrows, ncolumns;
00215 double *pr, *pi;
00216 int *imat;
00217 float *fmat;
00218 double *dmat;
00219 gs_scomplex *scmat;
00220 gs_dcomplex *dcmat;
00221 char *cmat;
00222
00223
00224 nrows = octval(index).rows();
00225 ncolumns = octval(index).columns();
00226
00227 if (argp->datatype == GS_CHAR) {
00228 if (!octval(index).is_string()) {
00229 fprintf(stderr, "Input argument %d should be a string\n", index);
00230 return -1;
00231 }
00232
00233 cmat = (char *) malloc(gs_datatype_sizeof(
00234 argp->datatype) * (nrows * ncolumns + 1));
00235 if (verify_allocation(cmat) < 0) return -1;
00236 for (int j = 0; j < ncolumns; j++) {
00237 for (int i = 0; i < nrows; i++) {
00238 cmat[i + j * nrows] = (char) (octval(index).char_matrix_value())(i, j);
00239 }
00240 }
00241 cmat[nrows * ncolumns] = '\0';
00242 argp->data = cmat;
00243 return 0;
00244 } else {
00245 pr = oct_GetPr(octval(index));
00246 pi = oct_GetPi(octval(index));
00247 }
00248
00249 switch (argp->datatype) {
00250 case GS_INT:
00251 imat = (int *) malloc(gs_datatype_sizeof(argp->datatype) * nrows * ncolumns);
00252 if (verify_allocation(imat) < 0) return -1;
00253 for (int i = 0; i < nrows * ncolumns; i++) {
00254 imat[i] = (int) pr[i];
00255 }
00256 argp->data = imat;
00257 break;
00258 case GS_FLOAT:
00259 fmat = (float *) malloc(gs_datatype_sizeof(argp->datatype) * nrows * ncolumns);
00260 if (verify_allocation(fmat) < 0) return -1;
00261 for (int i = 0; i < nrows * ncolumns; i++) {
00262 fmat[i] = (float) pr[i];
00263 }
00264 argp->data = fmat;
00265 break;
00266 case GS_DOUBLE:
00267 dmat = (double *) malloc(gs_datatype_sizeof(argp->datatype) * nrows * ncolumns);
00268 if (verify_allocation(dmat) < 0) return -1;
00269 for (int i = 0; i < nrows * ncolumns; i++) {
00270 dmat[i] = pr[i];
00271 }
00272 argp->data = dmat;
00273 break;
00274 case GS_SCOMPLEX:
00275 scmat = (gs_scomplex *) malloc(gs_datatype_sizeof(argp->datatype) * nrows * ncolumns);
00276 if (verify_allocation(scmat) < 0) return -1;
00277 for (int i = 0; i < nrows * ncolumns; i++) {
00278 scmat[i].r = (float) pr[i];
00279 scmat[i].i = (float) pi[i];
00280 }
00281 argp->data = scmat;
00282 break;
00283 case GS_DCOMPLEX:
00284 dcmat = (gs_dcomplex *) malloc(gs_datatype_sizeof(argp->datatype) * nrows * ncolumns);
00285 if (verify_allocation(dcmat) < 0) return -1;
00286 for (int i = 0; i < nrows * ncolumns; i++) {
00287 dcmat[i].r = pr[i];
00288 dcmat[i].i = pi[i];
00289 }
00290 argp->data = dcmat;
00291 break;
00292 case GS_CHAR:
00293 break;
00294 case GS_BAD_DTYPE:
00295 break;
00296 }
00297
00298 return 0;
00299 }
00300
00301
00302 int convert_sparse_matrix(gs_problem_t *pd, gs_argument_t *argp,
00303 octave_value_list octval, int index) {
00304 gs_argument_t *arg_indices;
00305 gs_argument_t *arg_pointer;
00306 int nrows, ncolumns, nnz;
00307 int *ir, *jc;
00308 double *pr, *pi;
00309 int *imat;
00310 float *fmat;
00311 double *dmat;
00312 gs_scomplex *scmat;
00313 gs_dcomplex *dcmat;
00314 char *cmat;
00315
00316
00317 nrows = octval(index).rows();
00318 ncolumns = octval(index).columns();
00319
00320 if (argp->datatype == GS_CHAR) {
00321 if (!octval(index).is_string()) {
00322 fprintf(stderr, "Input argument %d should be a string\n", index);
00323 return -1;
00324 }
00325
00326 cmat = (char *) malloc(gs_datatype_sizeof(
00327 argp->datatype) * (nrows * ncolumns + 1));
00328 if (verify_allocation(cmat) < 0) return -1;
00329 for (int j = 0; j < ncolumns; j++) {
00330 for (int i = 0; i < nrows; i++) {
00331 cmat[i + j * nrows] = (char) (octval(index).char_matrix_value())(i, j);
00332 }
00333 }
00334 cmat[nrows * ncolumns] = '\0';
00335 argp->data = cmat;
00336 return 0;
00337 } else {
00338 ir = oct_GetSparseIr(octval(index).matrix_value());
00339 jc = oct_GetSparseJc(octval(index).matrix_value());
00340 nnz = *(jc + ncolumns);
00341 pr = oct_GetSparsePr(octval(index).matrix_value());
00342 pi = oct_GetSparsePi(octval(index).matrix_value());
00343 }
00344
00345 arg_indices = gs_arg_lookup_by_name(argp->sparse_attr.indices, pd->arglist);
00346 arg_pointer = gs_arg_lookup_by_name(argp->sparse_attr.pointer, pd->arglist);
00347 if (arg_indices == NULL || arg_pointer == NULL) return -1;
00348 arg_indices->data = ir;
00349 arg_pointer->data = jc;
00350
00351 switch (argp->datatype) {
00352 case GS_INT:
00353 imat = (int *) malloc(gs_datatype_sizeof(argp->datatype) * nnz);
00354 if (verify_allocation(imat) < 0) return -1;
00355 for (int i = 0; i < nnz; i++) {
00356 imat[i] = (int) pr[i];
00357 }
00358 argp->data = imat;
00359 break;
00360 case GS_FLOAT:
00361 fmat = (float *) malloc(gs_datatype_sizeof(argp->datatype) * nnz);
00362 if (verify_allocation(fmat) < 0) return -1;
00363 for (int i = 0; i < nnz; i++) {
00364 fmat[i] = (float) pr[i];
00365 }
00366 argp->data = fmat;
00367 break;
00368 case GS_DOUBLE:
00369 dmat = (double *) malloc(gs_datatype_sizeof(argp->datatype) * nnz);
00370 if (verify_allocation(dmat) < 0) return -1;
00371 for (int i = 0; i < nnz; i++) {
00372 dmat[i] = pr[i];
00373 }
00374 argp->data = dmat;
00375 break;
00376 case GS_SCOMPLEX:
00377 scmat = (gs_scomplex *) malloc(gs_datatype_sizeof(argp->datatype) * nnz);
00378 if (verify_allocation(scmat) < 0) return -1;
00379 for (int i = 0; i < nnz; i++) {
00380 scmat[i].r = (float) pr[i];
00381 scmat[i].i = (float) pi[i];
00382 }
00383 argp->data = scmat;
00384 break;
00385 case GS_DCOMPLEX:
00386 dcmat = (gs_dcomplex *) malloc(gs_datatype_sizeof(argp->datatype) * nnz);
00387 if (verify_allocation(dcmat) < 0) return -1;
00388 for (int i = 0; i < nnz; i++) {
00389 dcmat[i].r = pr[i];
00390 dcmat[i].i = pi[i];
00391 }
00392 argp->data = dcmat;
00393 break;
00394 case GS_CHAR:
00395 break;
00396 case GS_BAD_DTYPE:
00397 break;
00398 }
00399
00400 return 0;
00401 }
00402
00403
00404 int convert_vector(gs_argument_t *argp, octave_value_list octval, int index) {
00405 int nrows, ncolumns, nelems;
00406 double *pr, *pi;
00407 int *ivec;
00408 float *fvec;
00409 double *dvec;
00410 gs_scomplex *scvec;
00411 gs_dcomplex *dcvec;
00412 char *cvec;
00413
00414
00415 nrows = octval(index).rows();
00416 ncolumns = octval(index).columns();
00417
00418 if (nrows != 1 && ncolumns != 1) {
00419 printf("Input argument %d should be a vector (1xN or Nx1)\n", index);
00420 return -1;
00421 }
00422
00423 nelems = (nrows == 1) ? ncolumns : nrows;
00424
00425 if (argp->datatype == GS_CHAR) {
00426 if (!octval(index).is_string()) {
00427 fprintf(stderr, "Input argument %d should be a string\n", index);
00428 return -1;
00429 }
00430
00431 cvec = (char *) malloc(gs_datatype_sizeof(
00432 argp->datatype) * (nelems + 1));
00433 if (verify_allocation(cvec) < 0) return -1;
00434 for (int i = 0; i < nelems; i++) {
00435 cvec[i] = (char) (octval(index).string_value())[i];
00436 }
00437 cvec[nelems] = '\0';
00438 argp->data = cvec;
00439 return 0;
00440 } else {
00441 pr = oct_GetPr(octval(index));
00442 pi = oct_GetPi(octval(index));
00443 }
00444
00445 switch (argp->datatype) {
00446 case GS_INT:
00447 ivec = (int *) malloc(gs_datatype_sizeof(argp->datatype) * nelems);
00448 if (verify_allocation(ivec) < 0) return -1;
00449 for (int i = 0; i < nelems; i++) {
00450 ivec[i] = (int) pr[i];
00451 }
00452 argp->data = ivec;
00453 break;
00454 case GS_FLOAT:
00455 fvec = (float *) malloc(gs_datatype_sizeof(argp->datatype) * nelems);
00456 if (verify_allocation(fvec) < 0) return -1;
00457 for (int i = 0; i < nelems; i++) {
00458 fvec[i] = (float) pr[i];
00459 }
00460 argp->data = fvec;
00461 break;
00462 case GS_DOUBLE:
00463 dvec = (double *) malloc(gs_datatype_sizeof(argp->datatype) * nelems);
00464 if (verify_allocation(dvec) < 0) return -1;
00465 for (int i = 0; i < nelems; i++) {
00466 dvec[i] = pr[i];
00467 }
00468 argp->data = dvec;
00469 break;
00470 case GS_SCOMPLEX:
00471 scvec = (gs_scomplex *) malloc(gs_datatype_sizeof(argp->datatype) * nelems);
00472 if (verify_allocation(scvec) < 0) return -1;
00473 for (int i = 0; i < nelems; i++) {
00474 scvec[i].r = (float) pr[i];
00475 scvec[i].i = (float) pi[i];
00476 }
00477 argp->data = scvec;
00478 break;
00479 case GS_DCOMPLEX:
00480 dcvec = (gs_dcomplex *) malloc(gs_datatype_sizeof(argp->datatype) * nelems);
00481 if (verify_allocation(dcvec) < 0) return -1;
00482 for (int i = 0; i < nelems; i++) {
00483 dcvec[i].r = pr[i];
00484 dcvec[i].i = pi[i];
00485 }
00486 argp->data = dcvec;
00487 break;
00488 case GS_CHAR:
00489 break;
00490 case GS_BAD_DTYPE:
00491 break;
00492 }
00493
00494 return 0;
00495 }
00496
00497
00498 int convert_scalar(gs_argument_t *argp, octave_value_list octval, int index) {
00499 int nrows, ncolumns;
00500 double *pr, *pi;
00501
00502
00503 nrows = octval(index).rows();
00504 ncolumns = octval(index).columns();
00505
00506 if (nrows != 1 || ncolumns != 1) {
00507 printf("Input argument %d should be a scalar\n", index);
00508 return -1;
00509 }
00510
00511 if (argp->datatype == GS_CHAR) {
00512 argp->scalar_val.char_val = (char) (octval(index).string_value())[0];
00513 argp->data = &(argp->scalar_val);
00514 return 0;
00515 } else {
00516 pr = oct_GetPr(octval(index));
00517 pi = oct_GetPi(octval(index));
00518 }
00519
00520 switch (argp->datatype) {
00521 case GS_INT:
00522 argp->scalar_val.int_val = (int) pr[0];
00523 break;
00524 case GS_FLOAT:
00525 argp->scalar_val.float_val = (float) pr[0];
00526 break;
00527 case GS_DOUBLE:
00528 argp->scalar_val.double_val = pr[0];
00529 break;
00530 case GS_SCOMPLEX:
00531 argp->scalar_val.scomplex_val.r = (float) pr[0];
00532 argp->scalar_val.scomplex_val.i = (float) pi[0];
00533 break;
00534 case GS_DCOMPLEX:
00535 argp->scalar_val.dcomplex_val.r = pr[0];
00536 argp->scalar_val.dcomplex_val.i = pi[0];
00537 break;
00538 case GS_CHAR:
00539 break;
00540 case GS_BAD_DTYPE:
00541 break;
00542 }
00543
00544 argp->data = &(argp->scalar_val);
00545
00546 return 0;
00547 }
00548
00549
00550 int convert_file_argument(gs_argument_t *argp, octave_value_list octval, int index) {
00551 if (!octval(index).is_string()) {
00552 fprintf(stderr, "Input argument %d should be a file name (string)\n");
00553 return -1;
00554 }
00555
00556 int nrows = octval(index).rows();
00557 int ncolumns = octval(index).columns();
00558
00559 char *fname = (char *) malloc(gs_datatype_sizeof(
00560 argp->datatype) * (nrows * ncolumns + 1));
00561 if (verify_allocation(fname) < 0) return -1;
00562
00563 for (int j = 0; j < ncolumns; j++) {
00564 for (int i = 0; i < nrows; i++) {
00565 fname[i + j * nrows] = (char) (octval(index).char_matrix_value())(i, j);
00566 }
00567 }
00568 fname[nrows * ncolumns] = '\0';
00569 argp->data = fname;
00570
00571 return 0;
00572 }
00573
00574
00575 int convert_packed_file_argument(gs_argument_t *argp, octave_value_list octval, int index) {
00576 if (!octval(index).is_string()) {
00577 fprintf(stderr, "Input argument %d should be a file name (string)\n");
00578 return -1;
00579 }
00580
00581 int nrows = octval(index).rows();
00582 int ncolumns = octval(index).columns();
00583
00584 if (nrows != argp->rows) {
00585 fprintf(stderr, "Bad string vector dimension for input packed file names\n");
00586 return -1;
00587 }
00588
00589 char **fnames = (char **) malloc(sizeof(char *) * nrows);
00590 if (verify_allocation(fnames) < 0) return -1;
00591 for (int i = 0; i < nrows; i++) {
00592 fnames[i] = (char *) malloc(sizeof(char) * (ncolumns + 1));
00593 if (verify_allocation(fnames[i]) < 0) return -1;
00594 for (int j = 0; j < ncolumns; j++) {
00595 fnames[i][j] = (char) (octval(index).char_matrix_value())(i, j);
00596 }
00597 fnames[i][ncolumns] = '\0';
00598 }
00599
00600 argp->data = fnames;
00601
00602 return 0;
00603 }
00604
00605
00606 int convert_output_objects(gs_problem_t* pd, octave_value_list &retvals) {
00607 gs_argument_t *argp;
00608
00609 for (argp = pd->arglist; argp != NULL; argp = argp->next) {
00610
00611 if (argp->inout == GS_IN || argp->inout == GS_WORKSPACE) continue;
00612 if (is_sparse_index_or_pointer(argp, pd)) continue;
00613
00614 switch (argp->objecttype) {
00615 case GS_MATRIX:
00616 if (fill_matrix(argp, retvals) < 0) return -1;
00617 break;
00618 case GS_SPARSEMATRIX:
00619 if (fill_sparse_matrix(pd, argp, retvals) < 0) return -1;
00620 break;
00621 case GS_VECTOR:
00622 if (fill_vector(argp, retvals) < 0) return -1;
00623 break;
00624 case GS_SCALAR:
00625 if (fill_scalar(argp, retvals) < 0) return -1;
00626 break;
00627 case GS_FILE:
00628 if (fill_file_argument(argp, retvals) < 0) return -1;
00629 break;
00630 case GS_PACKEDFILE:
00631 if (fill_packed_file_argument(argp, retvals) < 0) return -1;
00632 break;
00633 default:
00634 break;
00635 }
00636 }
00637 }
00638
00639
00640 int fill_matrix(gs_argument_t *argp, octave_value_list& retval) {
00641 int nrows, ncolumns;
00642
00643 nrows = argp->rows;
00644 ncolumns = argp->cols;
00645 Matrix mat = Matrix(nrows, ncolumns);
00646 ComplexMatrix cmat = ComplexMatrix(nrows, ncolumns);
00647 charMatrix chmat = charMatrix(nrows, ncolumns);
00648
00649 switch (argp->datatype) {
00650 case GS_INT:
00651 for (int i = 0; i < mat.columns(); i++) {
00652 for (int j = 0; j < mat.rows(); j++) {
00653 if (argp->inout == GS_VAROUT) {
00654 mat(j, i) = (int) (*(int **) argp->data)[i * mat.rows() + j];
00655 } else {
00656 mat(j, i) = (int) ((int *) argp->data)[i * mat.rows() + j];
00657 }
00658 }
00659 }
00660 retval.append(octave_value(mat));
00661 free(argp->data);
00662 break;
00663 case GS_FLOAT:
00664 for (int i = 0; i < mat.columns(); i++) {
00665 for (int j = 0; j < mat.rows(); j++) {
00666 if (argp->inout == GS_VAROUT) {
00667 mat(j, i) = (float) (*(float **) argp->data)[i * mat.rows() + j];
00668 } else {
00669 mat(j, i) = (float) ((float *) argp->data)[i * mat.rows() + j];
00670 }
00671 }
00672 }
00673 retval.append(octave_value(mat));
00674 free(argp->data);
00675 break;
00676 case GS_DOUBLE:
00677 for (int i = 0; i < mat.columns(); i++) {
00678 for (int j = 0; j < mat.rows(); j++) {
00679 if (argp->inout == GS_VAROUT) {
00680 mat(j, i) = (double) (*(double **) argp->data)[i * mat.rows() + j];
00681 } else {
00682 mat(j, i) = (double) ((double *) argp->data)[i * mat.rows() + j];
00683 }
00684 }
00685 }
00686 retval.append(octave_value(mat));
00687 free(argp->data);
00688 break;
00689 case GS_SCOMPLEX:
00690 for (int i = 0; i < cmat.columns(); i++) {
00691 for (int j = 0; j < cmat.rows(); j++) {
00692 int index = i * cmat.rows() + j;
00693 if (argp->inout == GS_VAROUT) {
00694 cmat(j, i) = Complex((*(gs_scomplex **) argp->data)[index].r,
00695 (*(gs_scomplex **) argp->data)[index].i);
00696 } else {
00697 cmat(j, i) = Complex(((gs_scomplex *) argp->data)[index].r,
00698 ((gs_scomplex *) argp->data)[index].i);
00699 }
00700 }
00701 }
00702 retval.append(octave_value(cmat));
00703 free(argp->data);
00704 break;
00705 case GS_DCOMPLEX:
00706 for (int i = 0; i < cmat.columns(); i++) {
00707 for (int j = 0; j < cmat.rows(); j++) {
00708 int index = i * cmat.rows() + j;
00709 if (argp->inout == GS_VAROUT) {
00710 cmat(j, i) = Complex((*(gs_dcomplex **) argp->data)[index].r,
00711 (*(gs_dcomplex **) argp->data)[index].i);
00712 } else {
00713 cmat(j, i) = Complex(((gs_dcomplex *) argp->data)[index].r,
00714 ((gs_dcomplex *) argp->data)[index].i);
00715 }
00716 }
00717 }
00718 retval.append(octave_value(cmat));
00719 free(argp->data);
00720 break;
00721 case GS_CHAR:
00722 for (int i = 0; i < mat.columns(); i++) {
00723 for (int j = 0; j < mat.rows(); j++) {
00724 if (argp->inout == GS_VAROUT) {
00725 mat(j, i) = (char) (*(char **) argp->data)[i * mat.rows() + j];
00726 } else {
00727 mat(j, i) = (char) ((char *) argp->data)[i * mat.rows() + j];
00728 }
00729 }
00730 }
00731 retval.append(octave_value(mat));
00732 free(argp->data);
00733 break;
00734 case GS_BAD_DTYPE:
00735 break;
00736 }
00737
00738 return 0;
00739 }
00740
00741
00742 int fill_sparse_matrix(gs_problem_t *pd, gs_argument_t *argp, octave_value_list& retval) {
00743 gs_argument_t *arg_pointer;
00744 gs_argument_t *arg_indices;
00745 int nnz, nrows, ncolumns;
00746 int *ir, *jc;
00747 Matrix mat;
00748 ComplexMatrix cmat;
00749 charMatrix chmat;
00750
00751
00752 nnz = argp->sparse_attr.nnz;
00753 nrows = argp->sparse_attr.rows_val_saved;
00754 ncolumns = argp->sparse_attr.cols_val_saved;;
00755 arg_indices = gs_arg_lookup_by_name(argp->sparse_attr.indices, pd->arglist);
00756 arg_pointer = gs_arg_lookup_by_name(argp->sparse_attr.pointer, pd->arglist);
00757 if (arg_indices == NULL || arg_pointer == NULL) return -1;
00758 ir = (int *) arg_indices->data;
00759 jc = (int *) arg_pointer->data;
00760
00761 switch (argp->datatype) {
00762 case GS_INT:
00763 mat = Matrix(nrows, ncolumns);
00764 for (int i = 0; i < nrows; i++)
00765 for (int j = 0; j < ncolumns; j++)
00766 mat(i, j) = 0;
00767
00768 for (int j; j < ncolumns; j++)
00769 for( int k = *(jc + j); k < *(jc + j + 1); k++)
00770 mat(*(ir + k), j) = (int) *((int *) argp->data + k);
00771
00772 retval.append(SparseMatrix(mat));
00773 break;
00774 case GS_FLOAT:
00775 mat = Matrix(nrows, ncolumns);
00776 for (int i = 0; i < nrows; i++)
00777 for (int j = 0; j < ncolumns; j++)
00778 mat(i, j) = 0.0;
00779
00780 for (int j; j < ncolumns; j++)
00781 for( int k = *(jc + j); k < *(jc + j + 1); k++)
00782 mat(*(ir + k), j) = (float) *((float *) argp->data + k);
00783
00784 retval.append(SparseMatrix(mat));
00785 break;
00786 case GS_DOUBLE:
00787 mat = Matrix(nrows, ncolumns);
00788 for (int i = 0; i < nrows; i++)
00789 for (int j = 0; j < ncolumns; j++)
00790 mat(i, j) = 0.00;
00791
00792 for (int j; j < ncolumns; j++)
00793 for( int k = *(jc + j); k < *(jc + j + 1); k++)
00794 mat(*(ir + k), j) = (double) *((double *) argp->data + k);
00795
00796 retval.append(SparseMatrix(mat));
00797 break;
00798 case GS_SCOMPLEX:
00799 cmat = ComplexMatrix(nrows, ncolumns);
00800 for (int i = 0; i < nrows; i++)
00801 for (int j = 0; j < ncolumns; j++)
00802 cmat(i, j) = 0;
00803
00804 for (int j; j < ncolumns; j++)
00805 for( int k = *(jc + j); k < *(jc + j + 1); k++)
00806 cmat(*(ir + k), j) = Complex(
00807 ((gs_scomplex) *((gs_scomplex *) argp->data + k)).r,
00808 ((gs_scomplex) *((gs_scomplex *) argp->data + k)).i);
00809
00810 retval.append(SparseComplexMatrix(cmat));
00811 break;
00812 case GS_DCOMPLEX:
00813 cmat = ComplexMatrix(nrows, ncolumns);
00814 for (int i = 0; i < nrows; i++)
00815 for (int j = 0; j < ncolumns; j++)
00816 cmat(i, j) = 0;
00817
00818 for (int j; j < ncolumns; j++)
00819 for( int k = *(jc + j); k < *(jc + j + 1); k++)
00820 cmat(*(ir + k), j) = Complex(
00821 ((gs_dcomplex) *((gs_dcomplex *) argp->data + k)).r,
00822 ((gs_dcomplex) *((gs_dcomplex *) argp->data + k)).i);
00823
00824 retval.append(SparseComplexMatrix(cmat));
00825 break;
00826 }
00827
00828 return 0;
00829 }
00830
00831
00832 int fill_vector(gs_argument_t *argp, octave_value_list& retval) {
00833 int nrows, ncolumns, nelems;
00834 int major;
00835 Matrix mat;
00836 ComplexMatrix cmat;
00837 charMatrix chmat;
00838
00839
00840 nrows = argp->rows;
00841 ncolumns = argp->cols;
00842
00843 if (nrows == 1) {
00844 mat = Matrix(1, ncolumns);
00845 cmat = ComplexMatrix(1, ncolumns);
00846 chmat = charMatrix(1, ncolumns);
00847 nelems = ncolumns;
00848 major = 0;
00849 }
00850 else if (ncolumns == 1) {
00851 mat = Matrix(nrows, 1);
00852 cmat = ComplexMatrix(nrows, 1);
00853 chmat = charMatrix(nrows, 1);
00854 nelems = nrows;
00855 major = 1;
00856 }
00857
00858 switch (argp->datatype) {
00859 case GS_INT:
00860 for (int i = 0; i < nelems; i++) {
00861 if (argp->inout == GS_VAROUT) {
00862 if (major == 0)
00863 mat(0, i) = (int) (*(int **) argp->data)[i];
00864 else
00865 mat(i, 0) = (int) (*(int **) argp->data)[i];
00866 } else {
00867 if (major == 0)
00868 mat(0, i) = (int) ((int *) argp->data)[i];
00869 else
00870 mat(i, 0) = (int) ((int *) argp->data)[i];
00871 }
00872 }
00873 free(argp->data);
00874 retval.append(octave_value(mat));
00875 break;
00876 case GS_FLOAT:
00877 for (int i = 0; i < nelems; i++) {
00878 if (argp->inout == GS_VAROUT) {
00879 if (major == 0)
00880 mat(0, i) = (float) (*(float **) argp->data)[i];
00881 else
00882 mat(i, 0) = (float) (*(float **) argp->data)[i];
00883 } else {
00884 if (major == 0)
00885 mat(0, i) = (float) ((float *) argp->data)[i];
00886 else
00887 mat(i, 0) = (float) ((float *) argp->data)[i];
00888 }
00889 }
00890 free(argp->data);
00891 retval.append(octave_value(mat));
00892 break;
00893 case GS_DOUBLE:
00894 for (int i = 0; i < nelems; i++) {
00895 if (argp->inout == GS_VAROUT) {
00896 if (major == 0)
00897 mat(0, i) = (double) (*(double **) argp->data)[i];
00898 else
00899 mat(i, 0) = (double) (*(double **) argp->data)[i];
00900 } else {
00901 if (major == 0)
00902 mat(0, i) = (double) ((double *) argp->data)[i];
00903 else
00904 mat(i, 0) = (double) ((double *) argp->data)[i];
00905 }
00906 }
00907 free(argp->data);
00908 retval.append(octave_value(mat));
00909 break;
00910 case GS_SCOMPLEX:
00911 for (int i = 0; i < nelems; i++) {
00912 if (argp->inout == GS_VAROUT) {
00913 if (major == 0)
00914 cmat(0, i) = Complex((*(gs_scomplex **) argp->data)[i].r,
00915 (*(gs_scomplex **) argp->data)[i].i);
00916 else
00917 cmat(i, 0) = Complex((*(gs_scomplex **) argp->data)[i].r,
00918 (*(gs_scomplex **) argp->data)[i].i);
00919 } else {
00920 if (major == 0)
00921 cmat(0, i) = Complex(((gs_scomplex *) argp->data)[i].r,
00922 ((gs_scomplex *) argp->data)[i].i);
00923 else
00924 cmat(i, 0) = Complex(((gs_scomplex *) argp->data)[i].r,
00925 ((gs_scomplex *) argp->data)[i].i);
00926 }
00927 }
00928 free(argp->data);
00929 retval.append(octave_value(cmat));
00930 break;
00931 case GS_DCOMPLEX:
00932 for (int i = 0; i < nelems; i++) {
00933 if (argp->inout == GS_VAROUT) {
00934 if (major == 0)
00935 cmat(0, i) = Complex((*(gs_dcomplex **) argp->data)[i].r,
00936 (*(gs_dcomplex **) argp->data)[i].i);
00937 else
00938 cmat(i, 0) = Complex((*(gs_dcomplex **) argp->data)[i].r,
00939 (*(gs_dcomplex **) argp->data)[i].i);
00940 } else {
00941 if (major == 0)
00942 cmat(0, i) = Complex(((gs_dcomplex *) argp->data)[i].r,
00943 ((gs_dcomplex *) argp->data)[i].i);
00944 else
00945 cmat(i, 0) = Complex(((gs_dcomplex *) argp->data)[i].r,
00946 ((gs_dcomplex *) argp->data)[i].i);
00947 }
00948 }
00949 free(argp->data);
00950 retval.append(octave_value(cmat));
00951 break;
00952 case GS_CHAR:
00953 if (argp->inout == GS_VAROUT) {
00954 char *str = strdup(*((char **) argp->data));
00955 retval.append(string_vector(str));
00956 free(*((char **) argp->data));
00957 free(argp->data);
00958 } else {
00959 char *str = strdup((char *) argp->data);
00960 retval.append(string_vector(str));
00961 free(argp->data);
00962 }
00963 break;
00964 case GS_BAD_DTYPE:
00965 break;
00966 }
00967
00968 return 0;
00969 }
00970
00971
00972 int fill_scalar(gs_argument_t *argp, octave_value_list& retval) {
00973 if (argp->rows != 1 || argp->cols != 1) return -1;
00974 Matrix mat = Matrix(1, 1);
00975 ComplexMatrix cmat = ComplexMatrix(1, 1);
00976 charMatrix chmat = charMatrix(1, 1);
00977
00978 switch (argp->datatype) {
00979 case GS_DOUBLE:
00980 mat(0, 0) = argp->scalar_val.double_val;
00981 retval.append(octave_value(mat));
00982 break;
00983 case GS_INT:
00984 mat(0, 0) = argp->scalar_val.int_val;
00985 retval.append(octave_value(mat));
00986 break;
00987 case GS_FLOAT:
00988 mat(0, 0) = argp->scalar_val.float_val;
00989 retval.append(octave_value(mat));
00990 break;
00991 case GS_SCOMPLEX:
00992 cmat(0, 0) = Complex(argp->scalar_val.scomplex_val.r,
00993 argp->scalar_val.scomplex_val.i);
00994 retval.append(octave_value(cmat));
00995 break;
00996 case GS_DCOMPLEX:
00997 cmat(0, 0) = Complex(argp->scalar_val.dcomplex_val.r,
00998 argp->scalar_val.dcomplex_val.i);
00999 retval.append(octave_value(cmat));
01000 break;
01001 case GS_CHAR:
01002 mat(0, 0) = (int) argp->scalar_val.char_val;
01003 retval.append(octave_value(mat));
01004 break;
01005 }
01006
01007 return 0;
01008 }
01009
01010
01011 int fill_file_argument(gs_argument_t *argp, octave_value_list& retval) {
01012 char *str = strdup((char *) argp->data);
01013 retval.append(string_vector(str));
01014 free(argp->data);
01015 return 0;
01016 }
01017
01018
01019 int fill_packed_file_argument(gs_argument_t *argp, octave_value_list& retval) {
01020 int ncolumns = 0;
01021 for (int i = 0; i < argp->rows; i++) {
01022 if (strlen(((char **) argp->data)[i]) > ncolumns);
01023 ncolumns = strlen(((char **) argp->data)[i]);
01024 }
01025
01026 charMatrix ret_packed_file_names = charMatrix(argp->rows, ncolumns);
01027 for (int i = 0; i < argp->rows; i++) {
01028 char *str = strdup(((char **) argp->data)[i]);
01029 for (int j = 0; j < strlen(str); j++)
01030 ret_packed_file_names.insert(str + j, i, j);
01031 free(((char **) argp->data)[i]);
01032 }
01033 free(argp->data);
01034 retval.append(ret_packed_file_names);
01035 return 0;
01036 }