Functions

oct_object_convert.cpp File Reference

#include <string>
#include <oct.h>
#include "grpc.h"
#include "gs_oct.h"
Include dependency graph for oct_object_convert.cpp:

Go to the source code of this file.

Functions

int verify_allocation (void *ptr)
int is_sparse_index_or_pointer (gs_argument_t *argp, gs_problem_t *pd)
int process_gs_out_arguments (gs_problem_t *pd)
int convert_octave_arguments (gs_problem_t *pd, octave_value_list oct_args)
int convert_matrix (gs_argument_t *argp, octave_value_list octval, int index)
int convert_sparse_matrix (gs_problem_t *pd, gs_argument_t *argp, octave_value_list octval, int index)
int convert_vector (gs_argument_t *argp, octave_value_list octval, int index)
int convert_scalar (gs_argument_t *argp, octave_value_list octval, int index)
int convert_file_argument (gs_argument_t *argp, octave_value_list octval, int index)
int convert_packed_file_argument (gs_argument_t *argp, octave_value_list octval, int index)
int convert_output_objects (gs_problem_t *pd, octave_value_list &retvals)
int fill_matrix (gs_argument_t *argp, octave_value_list &retval)
int fill_sparse_matrix (gs_problem_t *pd, gs_argument_t *argp, octave_value_list &retval)
int fill_vector (gs_argument_t *argp, octave_value_list &retval)
int fill_scalar (gs_argument_t *argp, octave_value_list &retval)
int fill_file_argument (gs_argument_t *argp, octave_value_list &retval)
int fill_packed_file_argument (gs_argument_t *argp, octave_value_list &retval)

Function Documentation

int convert_file_argument ( gs_argument_t *  argp,
octave_value_list  octval,
int  index 
)

Definition at line 550 of file oct_object_convert.cpp.

                                                                                    {
    if (!octval(index).is_string()) {
        fprintf(stderr, "Input argument %d should be a file name (string)\n");
        return -1;
    }

    int nrows = octval(index).rows();
    int ncolumns = octval(index).columns();
    
    char *fname = (char *) malloc(gs_datatype_sizeof(
        argp->datatype) * (nrows * ncolumns + 1));
    if (verify_allocation(fname) < 0) return -1;

    for (int j = 0; j < ncolumns; j++) {
        for (int i = 0; i < nrows; i++) {
            fname[i + j * nrows] = (char) (octval(index).char_matrix_value())(i, j);
        }
    }
    fname[nrows * ncolumns] = '\0';
    argp->data = fname;

    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int convert_matrix ( gs_argument_t *  argp,
octave_value_list  octval,
int  index 
)

Definition at line 213 of file oct_object_convert.cpp.

                                                                             {
    int nrows, ncolumns;
    double *pr, *pi;
    int *imat;
    float *fmat;
    double *dmat;
    gs_scomplex *scmat;
    gs_dcomplex *dcmat;
    char *cmat;


    nrows = octval(index).rows();
    ncolumns = octval(index).columns();

    if (argp->datatype == GS_CHAR) {
        if (!octval(index).is_string()) {
            fprintf(stderr, "Input argument %d should be a string\n", index);
            return -1;
        }

        cmat = (char *) malloc(gs_datatype_sizeof(
            argp->datatype) * (nrows * ncolumns + 1));
        if (verify_allocation(cmat) < 0) return -1;
        for (int j = 0; j < ncolumns; j++) {
            for (int i = 0; i < nrows; i++) {
                cmat[i + j * nrows] = (char) (octval(index).char_matrix_value())(i, j);
            }
        }
        cmat[nrows * ncolumns] = '\0';
        argp->data = cmat;
        return 0;
    } else {
        pr = oct_GetPr(octval(index));
        pi = oct_GetPi(octval(index));
    }

    switch (argp->datatype) {
        case GS_INT:
            imat = (int *) malloc(gs_datatype_sizeof(argp->datatype) * nrows * ncolumns);
            if (verify_allocation(imat) < 0) return -1;
            for (int i = 0; i < nrows * ncolumns; i++) {
                imat[i] = (int) pr[i];
            }
            argp->data = imat;
            break;
        case GS_FLOAT:
            fmat = (float *) malloc(gs_datatype_sizeof(argp->datatype) * nrows * ncolumns);
            if (verify_allocation(fmat) < 0) return -1;
            for (int i = 0; i < nrows * ncolumns; i++) {
                fmat[i] = (float) pr[i];
            }
            argp->data = fmat;
            break;
        case GS_DOUBLE:
            dmat = (double *) malloc(gs_datatype_sizeof(argp->datatype) * nrows * ncolumns);
            if (verify_allocation(dmat) < 0) return -1;
            for (int i = 0; i < nrows * ncolumns; i++) {
                dmat[i] = pr[i];
            }
            argp->data = dmat;
            break;
        case GS_SCOMPLEX:
            scmat = (gs_scomplex *) malloc(gs_datatype_sizeof(argp->datatype) * nrows * ncolumns);
            if (verify_allocation(scmat) < 0) return -1;
            for (int i = 0; i < nrows * ncolumns; i++) {
                scmat[i].r = (float) pr[i];
                scmat[i].i = (float) pi[i];
            }
            argp->data = scmat;
            break;
        case GS_DCOMPLEX:
            dcmat = (gs_dcomplex *) malloc(gs_datatype_sizeof(argp->datatype) * nrows * ncolumns);
            if (verify_allocation(dcmat) < 0) return -1;
            for (int i = 0; i < nrows * ncolumns; i++) {
                dcmat[i].r = pr[i];
                dcmat[i].i = pi[i];
            }
            argp->data = dcmat;
            break;
        case GS_CHAR:
            break;
        case GS_BAD_DTYPE:
            break;
    }
    
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int convert_octave_arguments ( gs_problem_t *  pd,
octave_value_list  oct_args 
)

Definition at line 147 of file oct_object_convert.cpp.

                                                                           {
    gs_argument_t *argp;

    int i = 1;
    //first process scalar input arguments
    for (argp = pd->arglist; argp != NULL; argp = argp->next) {

        if (argp->inout == GS_OUT ||
            argp->inout == GS_VAROUT ||
            argp->inout == GS_WORKSPACE) continue;
        if (is_sparse_index_or_pointer(argp, pd)) continue;

        if (argp->objecttype == GS_SCALAR) {
            if (convert_scalar(argp, oct_args, i) < 0) return -1;
        }
        i++;
    }
    
    //compute the size of each nonscalar input arguments
    if (gs_receiver_compute_arg_sizes(pd, GS_IN) < 0) {
        fprintf(stderr, "Failure to compute argument sizes\n");
        return -1;
    }

    i = 1;
    //then process nonscalar input arguments
    for (argp = pd->arglist; argp != NULL; argp = argp->next) {
        if (argp->inout == GS_OUT ||
            argp->inout == GS_VAROUT ||
            argp->inout == GS_WORKSPACE) continue;

        if (is_sparse_index_or_pointer(argp, pd)) {
            continue;
        }

        switch (argp->objecttype) {
            case GS_MATRIX:
                if (convert_matrix(argp, oct_args, i) < 0) return -1;
                break;
            case GS_SPARSEMATRIX:
                if (convert_sparse_matrix(pd, argp, oct_args, i) < 0) return -1;
                break;
            case GS_VECTOR:
                if (convert_vector(argp, oct_args, i) < 0) return -1;
                break;
            case GS_SCALAR:
                //Nothing to do here
                break;
            case GS_FILE:
                if (convert_file_argument(argp, oct_args, i) < 0) return -1;
                break;
            case GS_PACKEDFILE:
                if (convert_packed_file_argument(argp, oct_args, i) < 0) return -1;
                break;
            default:
                break;
        }

        i++;
    }
    
    //last, process GS_OUT arguments
    process_gs_out_arguments(pd);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int convert_output_objects ( gs_problem_t *  pd,
octave_value_list &  retvals 
)

Definition at line 606 of file oct_object_convert.cpp.

                                                                         {
    gs_argument_t *argp;

    for (argp = pd->arglist; argp != NULL; argp = argp->next) {
        //for input or workspace arguments, do nothing
        if (argp->inout == GS_IN || argp->inout == GS_WORKSPACE) continue;
        if (is_sparse_index_or_pointer(argp, pd)) continue;

        switch (argp->objecttype) {
            case GS_MATRIX:
                if (fill_matrix(argp, retvals) < 0) return -1;
                break;
            case GS_SPARSEMATRIX:
                if (fill_sparse_matrix(pd, argp, retvals) < 0) return -1;
                break;
            case GS_VECTOR:
                if (fill_vector(argp, retvals) < 0) return -1;
                break;
            case GS_SCALAR:
                if (fill_scalar(argp, retvals) < 0) return -1;
                break;
            case GS_FILE:
                if (fill_file_argument(argp, retvals) < 0) return -1;
                break;
            case GS_PACKEDFILE:
                if (fill_packed_file_argument(argp, retvals) < 0) return -1;
                break;
            default:
                break;
        }
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

int convert_packed_file_argument ( gs_argument_t *  argp,
octave_value_list  octval,
int  index 
)

Definition at line 575 of file oct_object_convert.cpp.

                                                                                           {
    if (!octval(index).is_string()) {
        fprintf(stderr, "Input argument %d should be a file name (string)\n");
        return -1;
    }

    int nrows = octval(index).rows();
    int ncolumns = octval(index).columns();

    if (nrows != argp->rows) {
        fprintf(stderr, "Bad string vector dimension for input packed file names\n");
        return -1;
    }

    char **fnames = (char **) malloc(sizeof(char *) * nrows);
    if (verify_allocation(fnames) < 0) return -1;
    for (int i = 0; i < nrows; i++) {
        fnames[i] = (char *) malloc(sizeof(char) * (ncolumns + 1));
        if (verify_allocation(fnames[i]) < 0) return -1;
        for (int j = 0; j < ncolumns; j++) {
            fnames[i][j] = (char) (octval(index).char_matrix_value())(i, j);
        }
        fnames[i][ncolumns] = '\0';
    }

    argp->data = fnames;

    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int convert_scalar ( gs_argument_t *  argp,
octave_value_list  octval,
int  index 
)

Definition at line 498 of file oct_object_convert.cpp.

                                                                             {
    int nrows, ncolumns;
    double *pr, *pi;


    nrows = octval(index).rows();
    ncolumns = octval(index).columns();

    if (nrows != 1 || ncolumns != 1) {
        printf("Input argument %d should be a scalar\n", index);
        return -1;
    }

    if (argp->datatype == GS_CHAR) {
        argp->scalar_val.char_val = (char) (octval(index).string_value())[0];
        argp->data = &(argp->scalar_val);
        return 0;
    } else {
        pr = oct_GetPr(octval(index));
        pi = oct_GetPi(octval(index));
    }

    switch (argp->datatype) {
        case GS_INT:
            argp->scalar_val.int_val = (int) pr[0];
            break;
        case GS_FLOAT:
            argp->scalar_val.float_val = (float) pr[0];
            break;
        case GS_DOUBLE:
            argp->scalar_val.double_val = pr[0];
            break;
        case GS_SCOMPLEX:
            argp->scalar_val.scomplex_val.r = (float) pr[0];
            argp->scalar_val.scomplex_val.i = (float) pi[0];
            break;
        case GS_DCOMPLEX:
            argp->scalar_val.dcomplex_val.r = pr[0];
            argp->scalar_val.dcomplex_val.i = pi[0];
            break;
        case GS_CHAR:
            break;
        case GS_BAD_DTYPE:
            break;
    }

    argp->data = &(argp->scalar_val);
    
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int convert_sparse_matrix ( gs_problem_t *  pd,
gs_argument_t *  argp,
octave_value_list  octval,
int  index 
)

Definition at line 302 of file oct_object_convert.cpp.

                                         {
    gs_argument_t *arg_indices;
    gs_argument_t *arg_pointer;
    int nrows, ncolumns, nnz;
    int *ir, *jc;
    double *pr, *pi;
    int *imat;
    float *fmat;
    double *dmat;
    gs_scomplex *scmat;
    gs_dcomplex *dcmat;
    char *cmat;
    

    nrows = octval(index).rows();
    ncolumns = octval(index).columns();

    if (argp->datatype == GS_CHAR) {
        if (!octval(index).is_string()) {
            fprintf(stderr, "Input argument %d should be a string\n", index);
            return -1;
        }

        cmat = (char *) malloc(gs_datatype_sizeof(
            argp->datatype) * (nrows * ncolumns + 1));
        if (verify_allocation(cmat) < 0) return -1;
        for (int j = 0; j < ncolumns; j++) {
            for (int i = 0; i < nrows; i++) {
                cmat[i + j * nrows] = (char) (octval(index).char_matrix_value())(i, j);
            }
        }
        cmat[nrows * ncolumns] = '\0';
        argp->data = cmat;
        return 0;
    } else {
        ir = oct_GetSparseIr(octval(index).matrix_value());
        jc = oct_GetSparseJc(octval(index).matrix_value());
        nnz = *(jc + ncolumns);
        pr = oct_GetSparsePr(octval(index).matrix_value());
        pi = oct_GetSparsePi(octval(index).matrix_value());
    }

    arg_indices = gs_arg_lookup_by_name(argp->sparse_attr.indices, pd->arglist);
    arg_pointer = gs_arg_lookup_by_name(argp->sparse_attr.pointer, pd->arglist);
    if (arg_indices == NULL || arg_pointer == NULL) return -1;
    arg_indices->data = ir;
    arg_pointer->data = jc;

    switch (argp->datatype) {
        case GS_INT:
            imat = (int *) malloc(gs_datatype_sizeof(argp->datatype) * nnz);
            if (verify_allocation(imat) < 0) return -1;
            for (int i = 0; i < nnz; i++) {
                imat[i] = (int) pr[i];
            }
            argp->data = imat;
            break;
        case GS_FLOAT:
            fmat = (float *) malloc(gs_datatype_sizeof(argp->datatype) * nnz);
            if (verify_allocation(fmat) < 0) return -1;
            for (int i = 0; i < nnz; i++) {
                fmat[i] = (float) pr[i];
            }
            argp->data = fmat;
            break;
        case GS_DOUBLE:
            dmat = (double *) malloc(gs_datatype_sizeof(argp->datatype) * nnz);
            if (verify_allocation(dmat) < 0) return -1;
            for (int i = 0; i < nnz; i++) {
                dmat[i] = pr[i];
            }
            argp->data = dmat;
            break;
        case GS_SCOMPLEX:
            scmat = (gs_scomplex *) malloc(gs_datatype_sizeof(argp->datatype) * nnz);
            if (verify_allocation(scmat) < 0) return -1;
            for (int i = 0; i < nnz; i++) {
                scmat[i].r = (float) pr[i];
                scmat[i].i = (float) pi[i];
            }
            argp->data = scmat;
            break;
        case GS_DCOMPLEX:
            dcmat = (gs_dcomplex *) malloc(gs_datatype_sizeof(argp->datatype) * nnz);
            if (verify_allocation(dcmat) < 0) return -1;
            for (int i = 0; i < nnz; i++) {
                dcmat[i].r = pr[i];
                dcmat[i].i = pi[i];
            }
            argp->data = dcmat;
            break;
        case GS_CHAR:
            break;
        case GS_BAD_DTYPE:
            break;
    }
    
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int convert_vector ( gs_argument_t *  argp,
octave_value_list  octval,
int  index 
)

Definition at line 404 of file oct_object_convert.cpp.

                                                                             {
    int nrows, ncolumns, nelems;
    double *pr, *pi;
    int *ivec;
    float *fvec;
    double *dvec;
    gs_scomplex *scvec;
    gs_dcomplex *dcvec;
    char *cvec;


    nrows = octval(index).rows();
    ncolumns = octval(index).columns();

    if (nrows != 1 && ncolumns != 1) {
        printf("Input argument %d should be a vector (1xN or Nx1)\n", index);
        return -1;
    }
    
    nelems = (nrows == 1) ? ncolumns : nrows;

    if (argp->datatype == GS_CHAR) {
        if (!octval(index).is_string()) {
            fprintf(stderr, "Input argument %d should be a string\n", index);
            return -1;
        }

        cvec = (char *) malloc(gs_datatype_sizeof(
            argp->datatype) * (nelems + 1));
        if (verify_allocation(cvec) < 0) return -1;
        for (int i = 0; i < nelems; i++) {
            cvec[i] = (char) (octval(index).string_value())[i];
        }
        cvec[nelems] = '\0';
        argp->data = cvec;
        return 0;
    } else {
        pr = oct_GetPr(octval(index));
        pi = oct_GetPi(octval(index));
    }

    switch (argp->datatype) {
        case GS_INT:
            ivec = (int *) malloc(gs_datatype_sizeof(argp->datatype) * nelems);
            if (verify_allocation(ivec) < 0) return -1;
            for (int i = 0; i < nelems; i++) {
                ivec[i] = (int) pr[i];
            }
            argp->data = ivec;
            break;
        case GS_FLOAT:
            fvec = (float *) malloc(gs_datatype_sizeof(argp->datatype) * nelems);
            if (verify_allocation(fvec) < 0) return -1;
            for (int i = 0; i < nelems; i++) {
                fvec[i] = (float) pr[i];
            }
            argp->data = fvec;
            break;
        case GS_DOUBLE:
            dvec = (double *) malloc(gs_datatype_sizeof(argp->datatype) * nelems);
            if (verify_allocation(dvec) < 0) return -1;
            for (int i = 0; i < nelems; i++) {
                dvec[i] = pr[i];
            }
            argp->data = dvec;
            break;
        case GS_SCOMPLEX:
            scvec = (gs_scomplex *) malloc(gs_datatype_sizeof(argp->datatype) * nelems);
            if (verify_allocation(scvec) < 0) return -1;
            for (int i = 0; i < nelems; i++) {
                scvec[i].r = (float) pr[i];
                scvec[i].i = (float) pi[i];
            }
            argp->data = scvec;
            break;
        case GS_DCOMPLEX:
            dcvec = (gs_dcomplex *) malloc(gs_datatype_sizeof(argp->datatype) * nelems);
            if (verify_allocation(dcvec) < 0) return -1;
            for (int i = 0; i < nelems; i++) {
                dcvec[i].r = pr[i];
                dcvec[i].i = pi[i];
            }
            argp->data = dcvec;
            break;
        case GS_CHAR:
            break;
        case GS_BAD_DTYPE:
            break;
    }
    
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int fill_file_argument ( gs_argument_t *  argp,
octave_value_list &  retval 
)

Definition at line 1011 of file oct_object_convert.cpp.

                                                                       {
    char *str = strdup((char *) argp->data);
    retval.append(string_vector(str));
    free(argp->data);
    return 0;
}

Here is the caller graph for this function:

int fill_matrix ( gs_argument_t *  argp,
octave_value_list &  retval 
)

Definition at line 640 of file oct_object_convert.cpp.

                                                                {
    int nrows, ncolumns;

    nrows = argp->rows;
    ncolumns = argp->cols;
    Matrix mat = Matrix(nrows, ncolumns);
    ComplexMatrix cmat = ComplexMatrix(nrows, ncolumns);
    charMatrix chmat = charMatrix(nrows, ncolumns);

    switch (argp->datatype) {
        case GS_INT:
            for (int i = 0; i < mat.columns(); i++) {
                for (int j = 0; j < mat.rows(); j++) {
                    if (argp->inout == GS_VAROUT) {
                        mat(j, i) = (int) (*(int **) argp->data)[i * mat.rows() + j];
                    } else {
                        mat(j, i) = (int) ((int *) argp->data)[i * mat.rows() + j];
                    }
                }
            }
            retval.append(octave_value(mat));
            free(argp->data);
            break;
        case GS_FLOAT:
            for (int i = 0; i < mat.columns(); i++) {
                for (int j = 0; j < mat.rows(); j++) {
                    if (argp->inout == GS_VAROUT) {
                        mat(j, i) = (float) (*(float **) argp->data)[i * mat.rows() + j];
                    } else {
                        mat(j, i) = (float) ((float *) argp->data)[i * mat.rows() + j];
                    }
                }
            }
            retval.append(octave_value(mat));
            free(argp->data);
            break;
        case GS_DOUBLE:
            for (int i = 0; i < mat.columns(); i++) {
                for (int j = 0; j < mat.rows(); j++) {
                    if (argp->inout == GS_VAROUT) {
                        mat(j, i) = (double) (*(double **) argp->data)[i * mat.rows() + j];
                    } else {
                        mat(j, i) = (double) ((double *) argp->data)[i * mat.rows() + j];
                    }
                }
            }
            retval.append(octave_value(mat));
            free(argp->data);
            break;
        case GS_SCOMPLEX:
            for (int i = 0; i < cmat.columns(); i++) {
                for (int j = 0; j < cmat.rows(); j++) {
                    int index = i * cmat.rows() + j;
                    if (argp->inout == GS_VAROUT) {
                        cmat(j, i) = Complex((*(gs_scomplex **) argp->data)[index].r,
                        (*(gs_scomplex **) argp->data)[index].i);
                    } else {
                        cmat(j, i) = Complex(((gs_scomplex *) argp->data)[index].r,
                        ((gs_scomplex *) argp->data)[index].i);
                    }
                }
            }
            retval.append(octave_value(cmat));
            free(argp->data);
            break;
        case GS_DCOMPLEX:
            for (int i = 0; i < cmat.columns(); i++) {
                for (int j = 0; j < cmat.rows(); j++) {
                    int index = i * cmat.rows() + j;
                    if (argp->inout == GS_VAROUT) {
                        cmat(j, i) = Complex((*(gs_dcomplex **) argp->data)[index].r,
                        (*(gs_dcomplex **) argp->data)[index].i);
                    } else {
                        cmat(j, i) = Complex(((gs_dcomplex *) argp->data)[index].r,
                        ((gs_dcomplex *) argp->data)[index].i);
                    }
                }
            }
            retval.append(octave_value(cmat));
            free(argp->data);
            break;
        case GS_CHAR:
            for (int i = 0; i < mat.columns(); i++) {
                for (int j = 0; j < mat.rows(); j++) {
                    if (argp->inout == GS_VAROUT) {
                        mat(j, i) = (char) (*(char **) argp->data)[i * mat.rows() + j];
                    } else {
                        mat(j, i) = (char) ((char *) argp->data)[i * mat.rows() + j];
                    }
                }
            }
            retval.append(octave_value(mat));
            free(argp->data);
            break;
        case GS_BAD_DTYPE:
            break;
    }

    return 0;
}

Here is the caller graph for this function:

int fill_packed_file_argument ( gs_argument_t *  argp,
octave_value_list &  retval 
)

Definition at line 1019 of file oct_object_convert.cpp.

                                                                              {
    int ncolumns = 0;
    for (int i = 0; i < argp->rows; i++) {
        if (strlen(((char **) argp->data)[i]) > ncolumns);
            ncolumns = strlen(((char **) argp->data)[i]);
    }

    charMatrix ret_packed_file_names = charMatrix(argp->rows, ncolumns);
    for (int i = 0; i < argp->rows; i++) {
        char *str = strdup(((char **) argp->data)[i]);
        for (int j = 0; j < strlen(str); j++)
            ret_packed_file_names.insert(str + j, i, j);
        free(((char **) argp->data)[i]);
    }
    free(argp->data);
    retval.append(ret_packed_file_names);
    return 0;
}

Here is the caller graph for this function:

int fill_scalar ( gs_argument_t *  argp,
octave_value_list &  retval 
)

Definition at line 972 of file oct_object_convert.cpp.

                                                                {
    if (argp->rows != 1 || argp->cols != 1) return -1;
    Matrix mat = Matrix(1, 1);
    ComplexMatrix cmat = ComplexMatrix(1, 1);
    charMatrix chmat = charMatrix(1, 1);
    
    switch (argp->datatype) {
        case GS_DOUBLE:
            mat(0, 0) = argp->scalar_val.double_val;
            retval.append(octave_value(mat));
            break;
        case GS_INT:
            mat(0, 0) = argp->scalar_val.int_val;
            retval.append(octave_value(mat));
            break;
        case GS_FLOAT:
            mat(0, 0) = argp->scalar_val.float_val;
            retval.append(octave_value(mat));
            break;
        case GS_SCOMPLEX:
            cmat(0, 0) = Complex(argp->scalar_val.scomplex_val.r,
                argp->scalar_val.scomplex_val.i);
            retval.append(octave_value(cmat));
            break;
        case GS_DCOMPLEX:
            cmat(0, 0) = Complex(argp->scalar_val.dcomplex_val.r,
                argp->scalar_val.dcomplex_val.i);
            retval.append(octave_value(cmat));
            break;
        case GS_CHAR:
            mat(0, 0) = (int) argp->scalar_val.char_val;
            retval.append(octave_value(mat));
            break;
    }
    
    return 0;
}

Here is the caller graph for this function:

int fill_sparse_matrix ( gs_problem_t *  pd,
gs_argument_t *  argp,
octave_value_list &  retval 
)

Definition at line 742 of file oct_object_convert.cpp.

                                                                                         {
    gs_argument_t *arg_pointer;
    gs_argument_t *arg_indices;
    int nnz, nrows, ncolumns;
    int *ir, *jc;
    Matrix mat;
    ComplexMatrix cmat;
    charMatrix chmat;


    nnz = argp->sparse_attr.nnz;
    nrows = argp->sparse_attr.rows_val_saved;
    ncolumns = argp->sparse_attr.cols_val_saved;;
    arg_indices = gs_arg_lookup_by_name(argp->sparse_attr.indices, pd->arglist);
    arg_pointer = gs_arg_lookup_by_name(argp->sparse_attr.pointer, pd->arglist);
    if (arg_indices == NULL || arg_pointer == NULL) return -1;
    ir = (int *) arg_indices->data;
    jc = (int *) arg_pointer->data;

    switch (argp->datatype) {
        case GS_INT:
            mat = Matrix(nrows, ncolumns);
            for (int i = 0; i < nrows; i++)
                for (int j = 0; j < ncolumns; j++)
                    mat(i, j) = 0;

            for (int j; j < ncolumns; j++)
                for( int k = *(jc + j); k < *(jc + j + 1); k++)
                    mat(*(ir + k), j) = (int) *((int *) argp->data + k);

            retval.append(SparseMatrix(mat));
            break;
        case GS_FLOAT:
            mat = Matrix(nrows, ncolumns);
            for (int i = 0; i < nrows; i++)
                for (int j = 0; j < ncolumns; j++)
                    mat(i, j) = 0.0;

            for (int j; j < ncolumns; j++)
                for( int k = *(jc + j); k < *(jc + j + 1); k++)
                    mat(*(ir + k), j) = (float) *((float *) argp->data + k);

            retval.append(SparseMatrix(mat));
            break;
        case GS_DOUBLE:
            mat = Matrix(nrows, ncolumns);
            for (int i = 0; i < nrows; i++)
                for (int j = 0; j < ncolumns; j++)
                    mat(i, j) = 0.00;

            for (int j; j < ncolumns; j++)
                for( int k = *(jc + j); k < *(jc + j + 1); k++)
                    mat(*(ir + k), j) = (double) *((double *) argp->data + k);

            retval.append(SparseMatrix(mat));
            break;
        case GS_SCOMPLEX:
            cmat = ComplexMatrix(nrows, ncolumns);
            for (int i = 0; i < nrows; i++) 
                for (int j = 0; j < ncolumns; j++)
                    cmat(i, j) = 0;

            for (int j; j < ncolumns; j++)
                for( int k = *(jc + j); k < *(jc + j + 1); k++)
                    cmat(*(ir + k), j) = Complex(
                        ((gs_scomplex) *((gs_scomplex *) argp->data + k)).r,
                        ((gs_scomplex) *((gs_scomplex *) argp->data + k)).i);

            retval.append(SparseComplexMatrix(cmat));
            break;
        case GS_DCOMPLEX:
            cmat = ComplexMatrix(nrows, ncolumns);
            for (int i = 0; i < nrows; i++) 
                for (int j = 0; j < ncolumns; j++)
                    cmat(i, j) = 0;

            for (int j; j < ncolumns; j++)
                for( int k = *(jc + j); k < *(jc + j + 1); k++)
                    cmat(*(ir + k), j) = Complex(
                        ((gs_dcomplex) *((gs_dcomplex *) argp->data + k)).r,
                        ((gs_dcomplex) *((gs_dcomplex *) argp->data + k)).i);

            retval.append(SparseComplexMatrix(cmat));
            break;
    }

    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int fill_vector ( gs_argument_t *  argp,
octave_value_list &  retval 
)

Definition at line 832 of file oct_object_convert.cpp.

                                                                {
    int nrows, ncolumns, nelems;
    int major;
    Matrix mat;
    ComplexMatrix cmat;
    charMatrix chmat;
    

    nrows = argp->rows;
    ncolumns = argp->cols;

    if (nrows == 1) {
        mat = Matrix(1, ncolumns);
        cmat = ComplexMatrix(1, ncolumns);
        chmat = charMatrix(1, ncolumns);
        nelems = ncolumns;
        major = 0;
    }
    else if (ncolumns == 1) {
        mat = Matrix(nrows, 1);
        cmat = ComplexMatrix(nrows, 1);
        chmat = charMatrix(nrows, 1);
        nelems = nrows;
        major = 1;
    }

    switch (argp->datatype) {
        case GS_INT:
            for (int i = 0; i < nelems; i++) {
                if (argp->inout == GS_VAROUT) {
                    if (major == 0)
                        mat(0, i) = (int) (*(int **) argp->data)[i];
                    else 
                        mat(i, 0) = (int) (*(int **) argp->data)[i];
                } else {
                    if (major == 0)
                        mat(0, i) = (int) ((int *) argp->data)[i];
                    else 
                        mat(i, 0) = (int) ((int *) argp->data)[i];
                }
            }
            free(argp->data);
            retval.append(octave_value(mat));
            break;
        case GS_FLOAT:
            for (int i = 0; i < nelems; i++) {
                if (argp->inout == GS_VAROUT) {
                    if (major == 0)
                        mat(0, i) = (float) (*(float **) argp->data)[i];
                    else 
                        mat(i, 0) = (float) (*(float **) argp->data)[i];
                } else {
                    if (major == 0)
                        mat(0, i) = (float) ((float *) argp->data)[i];
                    else 
                        mat(i, 0) = (float) ((float *) argp->data)[i];
                }
            }
            free(argp->data);
            retval.append(octave_value(mat));
            break;
        case GS_DOUBLE:
            for (int i = 0; i < nelems; i++) {
                if (argp->inout == GS_VAROUT) {
                    if (major == 0)
                        mat(0, i) = (double) (*(double **) argp->data)[i];
                    else 
                        mat(i, 0) = (double) (*(double **) argp->data)[i];
                } else {
                    if (major == 0)
                        mat(0, i) = (double) ((double *) argp->data)[i];
                    else 
                        mat(i, 0) = (double) ((double *) argp->data)[i];
                }
            }
            free(argp->data);
            retval.append(octave_value(mat));
            break;
        case GS_SCOMPLEX:
            for (int i = 0; i < nelems; i++) {
                if (argp->inout == GS_VAROUT) {
                    if (major == 0)
                        cmat(0, i) = Complex((*(gs_scomplex **) argp->data)[i].r,
                        (*(gs_scomplex **) argp->data)[i].i);
                    else 
                        cmat(i, 0) = Complex((*(gs_scomplex **) argp->data)[i].r,
                        (*(gs_scomplex **) argp->data)[i].i);
                } else {
                    if (major == 0)
                        cmat(0, i) = Complex(((gs_scomplex *) argp->data)[i].r,
                        ((gs_scomplex *) argp->data)[i].i);
                    else 
                        cmat(i, 0) = Complex(((gs_scomplex *) argp->data)[i].r,
                        ((gs_scomplex *) argp->data)[i].i);
                }
            }
            free(argp->data);
            retval.append(octave_value(cmat));
            break;
        case GS_DCOMPLEX:
            for (int i = 0; i < nelems; i++) {
                if (argp->inout == GS_VAROUT) {
                    if (major == 0)
                        cmat(0, i) = Complex((*(gs_dcomplex **) argp->data)[i].r,
                        (*(gs_dcomplex **) argp->data)[i].i);
                    else
                        cmat(i, 0) = Complex((*(gs_dcomplex **) argp->data)[i].r,
                        (*(gs_dcomplex **) argp->data)[i].i);
                } else {
                    if (major == 0)
                        cmat(0, i) = Complex(((gs_dcomplex *) argp->data)[i].r,
                        ((gs_dcomplex *) argp->data)[i].i);
                    else 
                        cmat(i, 0) = Complex(((gs_dcomplex *) argp->data)[i].r,
                        ((gs_dcomplex *) argp->data)[i].i);
                }
            }
            free(argp->data);
            retval.append(octave_value(cmat));
            break;
        case GS_CHAR:
            if (argp->inout == GS_VAROUT) {
                char *str = strdup(*((char **) argp->data));
                retval.append(string_vector(str));
                free(*((char **) argp->data));
                free(argp->data);
            } else {
                char *str = strdup((char *) argp->data);
                retval.append(string_vector(str));
                free(argp->data);
            }
            break;
        case GS_BAD_DTYPE:
            break;
    }

    return 0;
}

Here is the caller graph for this function:

int is_sparse_index_or_pointer ( gs_argument_t *  argp,
gs_problem_t *  pd 
)

Definition at line 32 of file oct_object_convert.cpp.

                                                                      {
    int is_index_or_pointer = 0;
    gs_argument_t* arg_attr;
    
    for (arg_attr = pd->arglist; arg_attr != NULL; arg_attr = arg_attr->next)
        if ((arg_attr->objecttype == GS_SPARSEMATRIX)  &&
           (!strcmp(argp->name, arg_attr->sparse_attr.indices) ||
            !strcmp(argp->name, arg_attr->sparse_attr.pointer))) {
            is_index_or_pointer = 1;
            break;
    }

    return is_index_or_pointer;
}

Here is the caller graph for this function:

int process_gs_out_arguments ( gs_problem_t *  pd  ) 

Definition at line 48 of file oct_object_convert.cpp.

                                               {
    gs_argument_t *argp;
    gs_argument_t *arg_indices;
    gs_argument_t *arg_pointer;
    void *v;
    int nrows, ncolumns;
    char buf[256];
    int fd;
    int nfiles;
    char **fnames;


    for (argp = pd->arglist; argp != NULL; argp = argp->next) {
        if (argp->inout != GS_OUT && argp->inout != GS_VAROUT) continue;
        if (is_sparse_index_or_pointer(argp, pd)) continue;

        switch (argp->objecttype) {
            case GS_MATRIX:
                if (argp->inout == GS_OUT) {
                    nrows = argp->rows;
                    ncolumns = argp->cols;
                    argp->data = (void *) malloc(gs_datatype_sizeof(
                        argp->datatype) * nrows * ncolumns);
                    if (verify_allocation(argp->data) < 0) return -1;
                }
                else if (argp->inout == GS_VAROUT) {
                    argp->data = (char **) malloc(sizeof(char *));
                }
                break;
            case GS_SPARSEMATRIX:
                if (argp->inout == GS_VAROUT) {
                    fprintf(stderr, "VAROUT SPARSEMATRIX arguments are not supported\n");
                    return -1;
                }
                arg_indices = gs_arg_lookup_by_name(argp->sparse_attr.indices, pd->arglist);
                arg_pointer = gs_arg_lookup_by_name(argp->sparse_attr.pointer, pd->arglist);
                if (arg_indices == NULL || arg_pointer == NULL) return -1;
                arg_indices->data = malloc(sizeof(int) * argp->sparse_attr.nnz);
                if (verify_allocation(arg_indices->data) < 0) return -1;
                arg_pointer->data = malloc(sizeof(int) * 
                    (argp->sparse_attr.cols_val_saved + 1));
                if (verify_allocation(arg_pointer->data) < 0) return -1;
                argp->data = (void *) malloc(argp->sparse_attr.nnz *
                    gs_datatype_sizeof(argp->datatype));
                if (verify_allocation(argp->data) < 0) return -1;
                break;
            case GS_VECTOR:
                if (argp->inout == GS_OUT) {
                    nrows = argp->rows;
                    argp->data = (void *) malloc(gs_datatype_sizeof(argp->datatype) * nrows);
                    if (verify_allocation(argp->data) < 0) return -1;
                }
                else if (argp->inout == GS_VAROUT) {
                    argp->data = (char **) malloc(sizeof(char *));
                }
                break;
            case GS_SCALAR:
                if (argp->inout == GS_OUT) {
                    argp->data = &(argp->scalar_val);
                }
                else if (argp->inout == GS_VAROUT) {
                    fprintf(stderr, "VAROUT SCALAR arguments are not supported\n");
                    return -1;
                }
                break;
            case GS_FILE:
                sprintf(buf, "%s_%s_OUT", pd->name, argp->name);
                fd =  open(buf, O_WRONLY | O_CREAT | O_TRUNC, 0600);
                if(fd < 0) {
                    fprintf(stderr, "Could not open file '%s'\n", buf);
                    return -1;
                }
                close(fd);
                argp->data = strdup(buf);
                if (argp->data == NULL) return -1;
                break;
            case GS_PACKEDFILE:
                nfiles = argp->rows * argp->cols;
                fnames = (char **) malloc(sizeof(char *) * nfiles);
                for (int i = 0; i < nfiles; i++) {
                    sprintf(buf, "%s_%s_%d_OUT", pd->name, argp->name, i + 1);
                    fd = open(buf, O_WRONLY | O_CREAT | O_TRUNC, 0600);
                    if(fd < 0) {
                        fprintf(stderr, "Could not open file '%s'\n", buf);
                        return -1;
                    }
                    close(fd);
                    fnames[i] = strdup(buf);
                    if (fnames[i] == NULL) return -1;
                }
                argp->data = fnames;
                break;
            default:
                break;
        }
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

int verify_allocation ( void *  ptr  ) 

Definition at line 22 of file oct_object_convert.cpp.

                                 {
    if (ptr == NULL) {
        perror("malloc");
        return -1;
    }

    return 0;
}

Here is the caller graph for this function: