LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Ansi C code works compiled with Dev-C++, but the same code is not working compiled with LabWin.CVI

Hi!
 
I had several days ago a question about converting between LabView-LabWinCVI binary files.(I have to read LabView file in LabWin).
I have my code finished, and I experianced really strange behaviour of the LabWinCVI compiled version.
Here is the ANSI-C code:
 
#include <ansi_c.h>
float max;
 
float SwapBytes (char *src)   /* 4 byteos szam LabView -->  LabWindows file formátum konvertere*/
{
 char tmp[4];
 tmp[0] = *(src + 3);
 tmp[1] = *(src + 2);
 tmp[2] = *(src + 1);
 tmp[3] = *(src + 0);
 return *(float *)tmp;
}
 
float fileatiro(FILE *ol)
{
 
 float data[1];
 char *raw = NULL;
 
    raw = malloc (sizeof(float));
   
    memset (raw, 0, sizeof(float));
 
 fread (raw, sizeof(float), 1, ol);
 
 data[0] = SwapBytes (raw);
 
 free (raw);
  
    return data[0];
}
void getMax(FILE *kk)
{  
  float ertek;
  
 max = fileatiro(kk);
  while (!feof(kk))
    {
      ertek = fileatiro(kk);
     
      if (max < ertek ) {
                  max = ertek;
                                 }
    }
}
int main (int argc, char *argv[])
{   FILE *ol;
  
   ol=fopen("aaa_z.dat", "rb");
 
   if (ol==NULL) {
      fprintf(stderr, "Sikertelen file-nyitasi kiserlet!\n");
      exit(-1);
             }
 
  
  
   getMax(ol);
   printf("A maximum: %f\n ", max);
  
 
   fclose(ol);
   //system("PAUSE");
    return 0;
}      
 
 
So, this code works in both DevC++ and LabWin compiled version, if i search for example in a binary file about 18kbytes, for the maximum value.
 But if I use them on a binary file about 40 Mbytes, the DevC++ compiled version still working ( takes about 10 seconds to calculate
 the max value) , but  the LabWindows version is not ( I was waiting about half an hour, but nothing).  So I just do not know why these strange results...small file works, big file doesnt?
 I was trying to look for the error, and i think the LabWindows version get frozen after several thousand numbers, but i am not
 sure... Why does the conventional ANSI-C compiler work, and LabWin doesnt?
 
Thank you for the help,
 
 best regards,
 
                  A. B-D. 
0 Kudos
Message 1 of 7
(3,908 Views)
Hi!
   I didn't understand what you're trying to do..... can you clarify?

   You execute the exe file generated by CVI as a standalone???

   Please, let me know.....

Matt

0 Kudos
Message 2 of 7
(3,893 Views)
I think you're seeing the differences between an optimizing compiler and a non-optimizing compiler.  CVI is a non-optimizing compiler. 

I ran you're code and it worked fine with a file containing 10 000 000 values.  It took approx 86 seconds to run on my system.  You could put a printf statement in your loop to print an incrementing counter to see if/where it fails.  You may be having trouble with malloc.  I noticed that you do not check the value returned from malloc to ensure that it is a valid pointer before using it.  malloc can fail. 

As an exercise, I took the malloc out and got a 2x improvement in speed.  Here's my updated function:

float fileatiro(FILE *ol)
{
 
 float data;
 char *raw = NULL;
    float aFloat = 0;
   
    // raw = malloc (sizeof(float));
    raw = (char *)&aFloat;
    //memset (raw, 0, sizeof(float));
 
 fread (raw, sizeof(float), 1, ol);
 
 data = SwapBytes (raw);
 
 //free (raw);
 
    return data;
}

There's no need to dynamically allocate a float each time this loop is entered.  Eliminating it saves time, and removes a point of failure.


----
I am the founder of CnCSoftwareSolutions. When not cleaning up baby drool, I write about test data or work on Vision, a tool for understanding your test data. Visit me at www.cncsoftwaresolutions.com
Message 3 of 7
(3,897 Views)
In release mode, the optimized code performs even better.  Using a 10 000 000 value dat file, the optimized code completes in ~5.5 seconds.  The malloc version in release mode completes in ~32 seconds.
----
I am the founder of CnCSoftwareSolutions. When not cleaning up baby drool, I write about test data or work on Vision, a tool for understanding your test data. Visit me at www.cncsoftwaresolutions.com
0 Kudos
Message 4 of 7
(3,891 Views)
More optimizations can be had by increasing the number of elements read in each loop.  To push the optimized code I created a 195MB .dat file.  It was processed in ~160 seconds.  This showed me that the application was IO bound as my processor utilization was under 20% and memory was under 50%.  Increasing the read size to 10 items reduced the time to about 6 seconds  for this file.  Increasing it to 100 items reduced the time to about 4 seconds.

I hope all this information helps.
----
I am the founder of CnCSoftwareSolutions. When not cleaning up baby drool, I write about test data or work on Vision, a tool for understanding your test data. Visit me at www.cncsoftwaresolutions.com
0 Kudos
Message 5 of 7
(3,884 Views)

Thank you very much! You helped me a lot!

Best regards,

 

                 András Bükki-Deme

0 Kudos
Message 6 of 7
(3,865 Views)
With CVI 8.0, we allow using third party optimized compilers directly from within CVI. This allows you to build release versions of your app using optimized compilers without leaving the CVI environment. You can use the existing templates we provide from some of the more popular compilers (Intel, MSVC, Borland).

The CVI 8.0 help topic "Creating Optimized Code" describes this in more detail.

I tried the non-malloc code to test this out with a file containing 100e6 floats (388Megs). I was reading one float at a time with the swapping.

Debug CVI build - 450 seconds
release build using CVI compiler - 49.7 seconds.
release build using VC 2003 compiler (downloadable for free, see below) - 47.5 seconds

http://msdn.microsoft.com/visualc/vctoolkit2003/

Hope this helps.

Bilal Durrani
NI
Message 7 of 7
(3,846 Views)