This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Optimizer Incorrect Code Generation

I have a fairly simple floating-point matrix-matrix multiply and accumulate function in C.  It compiles and runs successfully without optimization.  With function (-o2) and file (-o2) level optimization, it produces the incorrect output.

Here is the function:

typedef struct {
  int r, c; // number of rows and columns
  int capacity; // maximum number of elements that can be stored
// in the memory block pointed at by 'el'
  double *el; // storage for elements (row-wise)
} CMAT;

int cmat_mm_mac(CMAT* C, const CMAT* A, const CMAT* B)
{
	int _r, _c, id;
	
	// Check inputs:
	if (A->c != B->r || C->r != A->r || C->c != B->c) {
		return CMAT_ERR_SIZE;
	}
	// The output cannot be one of the inputs.
	if (A == C || B == C) {
		return CMAT_ERR_ARGS;	
	}
	
	for (_r=0; _r < C->r; _r++)		//	Outer dims
		for (_c=0; _c < C->c; _c++)	//	 form new matrix
			for (id=0; id < A->c; id++)	// Inner dimension 
				C->el[_r*C->c+_c] += A->el[_r*A->c+id] * B->el[id*B->c+_c];	
	
	return 0;
}


Here is a function to test the above:

#include <assert.h>
void testMMac(void)
{
   double A[] = { 
          0.2785, 0.9575, 0.1576,
          0.5469, 0.9649, 0.9706 
   }, B[] = { 
          0.9572, 0.1419, 0.4854,
          0.4218, 0.8003, 0.9157
   }, C[4] = { 0 }, Cexpected[] = {
          0.8575, 0.5877,
          1.7686, 1.3734
   };
   CMAT mA = { 2, 3, 6, NULL }, mB = { 3, 2, 6, NULL }, mC = { 2, 2, 4, NULL };
   int i;

   mA.el = A; mB.el = B; mC.el = C;

   cmat_mm_mac(&mC, &mA, &mB);
  
   for (i = 0; i < mC.r * mC.c; ++i) {
       printf("%.4f ", mC.el[i]);
       assert(fabs(mC.el[i] - Cexpected[i]) < TOL);
   }

   printf("\n\n");
}

The compiler options are -g -ml -mt -v28 --float_support=fpu32

Hope you can help; I hate turning off optimization for specific modules to get around bugs.

  • Thank you for a test case.  But I cannot compile it.  Things like CMAT_ERR_SIZE and TOL are not defined.  Please preprocess the code as described here, then post the resulting .pp file as an attachment.  I also need to know the compiler version number.

    Thanks and regards,

    -George

  • My apologies.  The attached file should compile.

     

  • The attachment didn't really seem to work.  I'll paste it inline.

    #include <math.h>
    #include <stdio.h>
    
    typedef struct {
       int r, c; // number of rows and columns
       int capacity; // maximum number of elements that can be stored
     // in the memory block pointed at by 'el'
       double *el; // storage for elements (row-wise)
    } CMAT;
    
    int cmat_mm_mac(CMAT* C, const CMAT* A, const CMAT* B)
    {
    	int _r, _c, id;
    	
    	// Check inputs:
    	if (A->c != B->r || C->r != A->r || C->c != B->c) {
    		return 1;
    	}
    	// The output cannot be one of the inputs.
    	if (A == C || B == C) {
    		return 1;	
    	}
    	
    	for (_r=0; _r < C->r; _r++)		//	Outer dims
    		for (_c=0; _c < C->c; _c++)	//	 form new matrix
    			for (id=0; id < A->c; id++)	// Inner dimension 
    				C->el[_r*C->c+_c] += A->el[_r*A->c+id] * B->el[id*B->c+_c];	
    	
    	return 0;
    }
    
    
    // Here is a function to test the above: 
    #include <assert.h>
    
    #define TOL     1e-4
    void testMMac(void)
    {
       double A[] = { 
              0.2785, 0.9575, 0.1576,
              0.5469, 0.9649, 0.9706 
       }, B[] = { 
              0.9572, 0.1419, 0.4854,
              0.4218, 0.8003, 0.9157
       }, C[4] = { 0 }, Cexpected[] = {
              0.8575, 0.5877,
              1.7686, 1.3734
       };
       CMAT mA = { 2, 3, 6, NULL }, mB = { 3, 2, 6, NULL }, mC = { 2, 2, 4, NULL };
       int i;
    
       mA.el = A; mB.el = B; mC.el = C;
    
       cmat_mm_mac(&mC, &mA, &mB);
      
       for (i = 0; i < mC.r * mC.c; ++i) {
           printf("%.4f ", mC.el[i]);
           assert(fabs(mC.el[i] - Cexpected[i]) < TOL);
       }
    
       printf("\n\n");
    }
    
    
    

     

  • And I've observed the error both with 5.2.7 and 5.2.11 of the code generation tools.

    Thanks,

    Bill 

  • I can verify that this problem occurs in 5.2.11.  The error margin is pretty severe.  This should be considered a bug.

    The test case does not fail when using 6.0.0B1.  It may have been fixed already.  This will require some investigation. 

  • This is now SDSCM00043061

  •  

    Is there any way for me to investigate the status of this bug report?  Somewhere I can lookup SDSCM00043061  ?

     

    Regards,

    Bill 

  • Bill Finger said:

    Is there any way for me to investigate the status of this bug report?  Somewhere I can lookup SDSCM00043061  ?

    You can use the SDOWP link in my signature to look up status of bug reports. Type in the bug number in the text box at the top left side of the page.

    You will notice that this bug has been "declined". I looked up more details internally, and analysis showed that the bug does not exist on v6.0.x or 6.1.x releases of the C2000 compiler tools, which are the current "active" releases, so you should not see the bug if you upgrade to one of these releases.

  •  AartiG,

     Thanks for your response.  I'll try the new compilers.

    The SDOWP link gave me "Could not connect to remote server".

    Regards,

    Bill