CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2001
    Location
    Kerala,India
    Posts
    650

    Write Matrix in file faster

    hi all i have 3050 x 3050 double matrix which is created through some calculations. This is a c application we got as a pilot project and we need to show them run faster. we have to first read this matrix from file which is CSV file and after some calculation write this back to file. Its taking more time in reading and writing the matrix. can any one give me a good algorithm for reading and writing this 3050 x 3050 double matrix. This is really a bottle neck for us, can any one help us.
    Do rate this post if it find useful to you

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Write Matrix in file faster

    Quote Originally Posted by Vinod S View Post
    can any one give me a good algorithm for reading and writing this 3050 x 3050 double matrix.
    There is only one algorithm: you read each value in sequence. If you want suggestions how to optimize your code, you'll have to post it first.
    In the mean time, have a look at this thread: http://www.codeguru.com/forum/showthread.php?t=507380
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Nov 2001
    Location
    Kerala,India
    Posts
    650

    Re: Write Matrix in file faster

    Code:
    		// Open file to read (will fail if file does not exist)
    		if( NULL == ( fStream  = fopen( cpFileName, "r" )))
    		{
    			perror( "Cannot open file to read : " ) ;
    			break ;
    		}
    
    		stFileOppend = 1 ;
    
    		stpData -> nRow = nRows ;
    		stpData -> nColumn = nColumns ;
    		stpData -> dbpMatrix = ( double ** ) malloc ( sizeof ( double * ) * nRows )  ;
    		
    		nRow = 0 ;
    		while ( NULL != fgets( cpReadString, 6144, fStream ) && nRow < nRows )
    		{
    			stpData -> dbpMatrix[nRow] = ( double * ) malloc ( sizeof ( double ) * nColumns ) ;
    
    			nColumn = 0 ;
    			cpSubString = strtok(cpReadString, "," ) ;
    			//fValue =  atof( Trim( cpSubString )) ;
    			//stpData -> fpMatrix[nRow][nColumn] = fValue ;
    
    			while ( NULL != cpSubString && nColumn < nColumns )
    			{
    				fValue =  atof( Trim( cpSubString )) ;
    				stpData -> dbpMatrix[nRow][nColumn] = fValue ;
    
    				++nColumn ;
    				cpSubString = strtok( NULL, "," ) ;
    			}
    			++nRow ; 
    		}
    		free ( cpReadString ) ;
    This is the code to read matrix

    Code:
    		// Open file to read (will fail if file does not exist)
    		if( NULL == ( fStream  = fopen( cpFileName, "w" ))) 
    		{
    			perror( "Cannot open file to write : " ) ;
    			break ;
    		}
    
    		stFileOppend = 0 ;
    
    		for ( nRow = 0 ; nRow < stpMg1g1 -> nRow ; ++nRow )
    		{
    			//sprintf( cpFileContent, "" ) ;
    
    			for ( nColumn = 0 ; nColumn < stpMg1g1 -> nColumn ; ++nColumn  )
    			{
    				if ( stpMg1g1 -> dbpMG1G1_data[nRow][nColumn] == 0 ) sprintf( cpWriteText, "0" ) ;
    				else
    				{
    					sprintf( cpWriteText, "%.8f", stpMg1g1 -> dbpMG1G1_data[nRow][nColumn] ) ;
    					TrimTrailingZeorWithDot( cpWriteText ) ;
    				}
    
    				//strcat( cpFileContent, cpWriteText  ) ;
    				fputs( cpWriteText, fStream ) ;
    
    				if ( nColumn < stpMg1g1 -> nColumn - 1 ) 
    				{
    					//strcat( cpFileContent, ","  ) ;
    					fputs( ",", fStream ) ;
    				}
    			}
    
    			//if ( nRow <= stpMg1g1 -> nRow - 1 )
    			{
    				fputs( "\n", fStream ) ; 
    				//strcat( cpFileContent, "\n"  ) ;
    			}
    				
    			//fputs( cpFileContent, fStream ) ; 
    		}
    This is the code i am using to write can you specify a better method.
    Do rate this post if it find useful to you

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Write Matrix in file faster

    If you're really into speed, I'd suggest to migrate from CSV format to your own binary matrix file format. This would bypass parsing and conversion of the doubles between text and binary representations which certainly consume the vast majority of processing time. From your short description of the scenario, I can't tell whether this actually is an option, though.

    If you get the original input file from an external source and you can't influence the format in which you get it, it may be worth to implement an extra step that converts the CSV representation into the binary matrix format. Of course that would only pay if you're going to read in that particular matrix multiple times, but then it would pay big-time...

    I have no idea whether an approach like this has been suggested it that other thread linked to by D Drmmr as well - I'm simply too lazy to read all those 77 posts... However, chances are that it actually was suggested there, since it's a quite common approach in scenarios like this.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Write Matrix in file faster

    Quote Originally Posted by Vinod S View Post
    Code:
    		while ( NULL != fgets( cpReadString, 6144, fStream ) && nRow < nRows )
    That looks suspicious. Are you sure that 6142 characters is enough to represent a full row of 3050 values?

    I didn't see any things in your code that would surely cause a big performance hit. However, some things that may help:
    - Allocate the data for the entire matrix in one contiguous block, rather than one block for each row. You can allocate and keep an array of pointers to the first element in each row for fast random access using two indices.
    - There's no need to trim a string before reading a value from it.
    - It may be faster to keep a pointer to the point in the array where you want to store a value and increment the pointer in each iteration of the loop.
    - When you write the matrix, you call fputs twice in each iteration. It may be faster to build a string for each line and write that.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Write Matrix in file faster

    Quote Originally Posted by Eri523 View Post
    I have no idea whether an approach like this has been suggested it that other thread linked to by D Drmmr as well - I'm simply too lazy to read all those 77 posts...
    Post #2 contains all the relevant ideas, the rest are just details.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  7. #7
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Write Matrix in file faster

    Quote Originally Posted by D_Drmmr View Post
    Post #2 contains all the relevant ideas, the rest are just details.
    Thanks.

    The improvement OReubens reports as the result of applying his changes is awesome! However, I'm not sure whether overlapped I/O and double-buffering still would yield that much a gain, given disk performance and caching strategies of a modern system. Yet it's definitely worth a try, in particular if the CSV format is mandatory.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured