help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

files or pipes use for data exchange? or something else?


From: Marco Driusso
Subject: files or pipes use for data exchange? or something else?
Date: Tue, 24 Mar 2009 18:57:12 +0100

Dear all,
I'm embedding octave in a c++ program. I use the octave_embed packet to initialize and call the octave functions I need, but in this packet I haven't found a way to pass to octave my c++ variables. My first own solution was:
- c++ code -> octave: convert my double arrays in string and evaluate them with the octave_embed's
octave_call(char*)
so the evaluated strings became variables in the octave workspace;
- octave -> c++ code: save the elaborated variables in a file with the octave's save and read them from the c++ code with operator >>.

In my c++ program I have to call the octave functions many times, so this way was not efficient (because implies write and read into/from the file sistem many times). So I thought that using named pipes (fifos) was a better solution:
- c++ code -> octave:
   
    FILE* fifoW;
    pid_t pid=fork();

    if(pid==0)
    {
        fifoW=fopen("/../fifo","w");
        fwrite(datas,sizeof(double),tot,fifoW);//datas is a double array of dimentions tot
        fclose(fifoW);
        exit(1);
    }
    else if(pid>0)
    {
        octave_call("fifoR = fopen(\"/../fifo\",\"r\");");
        octave_call("data="">         octave_call("fclose(fifoR);");
    }

- octave -> c++ code:
    FILE* fifoR;
    pid_t pid=fork();
   
    if(pid==0)
    {
        octave_call("fifoW = fopen(\"/../fifo\",\"w\");");
        octave_call("scrivo = fwrite(fifoW,elData,\"double\");");
        octave_call("fclose(fifoW);");
        exit(1);
    }
    else if(pid>0)
    {
        fifoR=fopen("/../fifo","r");
        fread(elData,sizeof(double),dim,fifoR);//elData is a double array of diment dim
        fclose(fifoR);
    }
(I've omitted all error check). Timing this and the other solution I've founded that the pipe solution is slower than the file solution and I don't know why. The two solution take similar times when the dimentions of the arrays became bigger (but the file solution remane faster than the  pipe solution). The questions are:
-Does anyone know why pipes are in this case slower than files?
-Has anyone a better solution than pipes or files?
Thanks.

Marco
reply via email to

[Prev in Thread] Current Thread [Next in Thread]