Thread: C code optimization

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    52

    Unhappy C code optimization

    I have executed the following codes in Hardware and compared timing cycles. But timing cycles and latency are more, so i need to optimize my codes. but my skill level is not sufficient enough to optimize to make it better/faster.

    Code:
    #include <stdio.h>
     
    #define ROWS 4
    #define COLS 8
     
    int main()
    {
        int i,j,k,temp;
        int matrix[ROWS][COLS]={{0,1,0,1,1,0,0,1},{1,1,1,0,0,1,0,0},{0,0,1,0,0,1,1,1},{1,0,0,1,1,0,1,0}};
        int opMatrix[ROWS][COLS];
        int countArray[ROWS]={0};  //this array is used for knowing how many number of 1's present in each row of matrix
        int fArray[ROWS]={0};
        int receivedCodeWord[COLS]={1,1,0,1,0,1,0,1};
        int originalCodeWord[COLS];
        int dout[COLS];
        int onecount = 0,zerocount = 0;
        //initializing the opMatrix to -1
        for(i=0;i<ROWS;i++)
                for(j=0;j<COLS;j++)
                        opMatrix[i][j]=-1;
       
       
     
    printf("H Matrix is: \n\n");
    for(i=0;i<ROWS;i++)
    {
        for(j=0;j<COLS;j++)
            printf("%d\t",matrix[i][j]);
        printf("\n");
    }
       
       
       
        for(i=0;i<ROWS;i++)
        {
            temp = 0;
            for(j=0;j<COLS;j++)
            {
                   if(matrix[i][j] == 1)
                    {
                        opMatrix[i][temp++] = j;   
                       
                    }
            }
            countArray[i] = temp; 
        }
       
       
    
        for(i=0;i<ROWS;i++)
        {
        int temp,val;
            if(countArray[i] == 1)
            {
                    temp = opMatrix[i][0];
                    fArray[i]=receivedCodeWord[temp];
       
            }
            else
            {
                temp = opMatrix[i][0];
                val = receivedCodeWord[temp];
                for(j = 1;j<countArray[i];j++)
                {
                    temp = opMatrix[i][j];
                    temp =  receivedCodeWord[temp]; 
                    val = val ^ temp;
                }
                fArray[i] = val;
            }
     
         }
        
    
        for(i=0;i<COLS;i++)
            originalCodeWord[i] = receivedCodeWord[i];
       
        for(k=0;k<COLS;k++)
        {
           
            originalCodeWord[k] == 0 ? zerocount++ : onecount++;
           
            for(i=0;i<ROWS;i++)
            {
                int flag = 0;  //to know whether that particular c is connected to f
                for(j=0;j<COLS;j++)
                {
                    if(opMatrix[i][j]==k)
                    {
                      flag = 1;
                      break;
                    }
               
                }
                if(flag == 1)
                {
                    temp = fArray[i];
                    int temp2 = originalCodeWord[k];
                    if(temp == 1)
                    {
                        temp2 = temp2 ^ 1;
                    }
                   temp2==0?zerocount++:onecount++;
                   
                 }
           
           
               }
               if(zerocount>onecount)
    {
        originalCodeWord[k] = 0;
    }
    else
    {
    originalCodeWord[k] = 1;
     
    }
    originalCodeWord[k] = 1;
              
        }
       
    
       printf("OriginalCodeWord is: \n\n");
        for(i=0;i<COLS;i++)
        {
           dout[i] =originalCodeWord[i];
                printf("%d ",dout[i]);
    
     
     
    }
        return 0;
    }
    Please anyone help me

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For starters, you need to indent the code properly, e.g.,
    Code:
    #include <stdio.h>
    
    #define ROWS 4
    #define COLS 8
    
    int main()
    {
        int i, j, k, temp;
        int matrix[ROWS][COLS] = {{0, 1, 0, 1, 1, 0, 0, 1},
                                  {1, 1, 1, 0, 0, 1, 0, 0},
                                  {0, 0, 1, 0, 0, 1, 1, 1},
                                  {1, 0, 0, 1, 1, 0, 1, 0}};
        int opMatrix[ROWS][COLS];
        //this array is used for knowing how many number of 1's present in each row of matrix:
        int countArray[ROWS] = {0};
        int fArray[ROWS] = {0};
        int receivedCodeWord[COLS] = {1, 1, 0, 1, 0, 1, 0, 1};
        int originalCodeWord[COLS];
        int dout[COLS];
        int onecount = 0, zerocount = 0;
    
        //initializing the opMatrix to -1
        for (i = 0; i < ROWS; i++)
            for (j = 0; j < COLS; j++)
                opMatrix[i][j] = -1;
    
        printf("H Matrix is: \n\n");
        for (i = 0; i < ROWS; i++)
        {
            for (j = 0; j < COLS; j++)
                printf("%d\t", matrix[i][j]);
            printf("\n");
        }
    
        for (i = 0; i < ROWS; i++)
        {
            temp = 0;
            for (j = 0; j < COLS; j++)
            {
                if (matrix[i][j] == 1)
                {
                    opMatrix[i][temp++] = j;
                }
            }
            countArray[i] = temp;
        }
    
        for (i = 0; i < ROWS; i++)
        {
            int temp, val;
            if (countArray[i] == 1)
            {
                temp = opMatrix[i][0];
                fArray[i] = receivedCodeWord[temp];
            }
            else
            {
                temp = opMatrix[i][0];
                val = receivedCodeWord[temp];
                for (j = 1; j < countArray[i]; j++)
                {
                    temp = opMatrix[i][j];
                    temp = receivedCodeWord[temp];
                    val = val ^ temp;
                }
                fArray[i] = val;
            }
        }
    
        for (i = 0; i < COLS; i++)
            originalCodeWord[i] = receivedCodeWord[i];
    
        for (k = 0; k < COLS; k++)
        {
            originalCodeWord[k] == 0 ? zerocount++ : onecount++;
    
            for (i = 0; i < ROWS; i++)
            {
                int flag = 0;  //to know whether that particular c is connected to f
                for (j = 0; j < COLS; j++)
                {
                    if (opMatrix[i][j] == k)
                    {
                        flag = 1;
                        break;
                    }
                }
    
                if (flag == 1)
                {
                    temp = fArray[i];
                    int temp2 = originalCodeWord[k];
                    if (temp == 1)
                    {
                        temp2 = temp2 ^ 1;
                    }
                    temp2 == 0 ? zerocount++ : onecount++;
                }
            }
    
            if (zerocount > onecount)
            {
                originalCodeWord[k] = 0;
            }
            else
            {
                originalCodeWord[k] = 1;
            }
    
            originalCodeWord[k] = 1;
        }
    
        printf("OriginalCodeWord is: \n\n");
        for (i = 0; i < COLS; i++)
        {
            dout[i] = originalCodeWord[i];
            printf("%d ", dout[i]);
        }
    
        return 0;
    }
    Quote Originally Posted by SarasMuthu
    But timing cycles and latency are more, so i need to optimize my codes. but my skill level is not sufficient enough to optimize to make it better/faster.
    Did you compile at a high optimisation level? Are you implementing the same algorithm?

    Incidentally, this looks strange:
    Code:
    if (zerocount > onecount)
    {
        originalCodeWord[k] = 0;
    }
    else
    {
        originalCodeWord[k] = 1;
    }
    
    originalCodeWord[k] = 1;
    as it can be simplified to just:
    Code:
    originalCodeWord[k] = 1;
    Perhaps before you optimise you should check if your code is correct.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    52
    Thanks for your response.

    I am doing Bit_flippling. I am findind whether ZERO count is high or ONE COUNT IS HIGH.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Taking laserlight's nicely formatted code.
    Code:
    $ gcc --coverage foo.c
    $ ./a.out 
    H Matrix is: 
    
    0	1	0	1	1	0	0	1	
    1	1	1	0	0	1	0	0	
    0	0	1	0	0	1	1	1	
    1	0	0	1	1	0	1	0	
    OriginalCodeWord is: 
    
    1 1 1 1 1 1 1 1 $ 
    $ gcov a.out
    a.gcno:cannot open notes file
    $ gcov foo.c
    File 'foo.c'
    Lines executed:96.43% of 56
    Creating 'foo.c.gcov'
    
    $ cat foo.c.gcov
            -:    0:Source:foo.c
            -:    0:Graph:foo.gcno
            -:    0:Data:foo.gcda
            -:    0:Runs:1
            -:    0:Programs:1
            -:    1:#include <stdio.h>
            -:    2:
            -:    3:#define ROWS 4
            -:    4:#define COLS 8
            -:    5:
            1:    6:int main()
            -:    7:{
            -:    8:    int i, j, k, temp;
            1:    9:    int matrix[ROWS][COLS] = {{0, 1, 0, 1, 1, 0, 0, 1},
            -:   10:                              {1, 1, 1, 0, 0, 1, 0, 0},
            -:   11:                              {0, 0, 1, 0, 0, 1, 1, 1},
            -:   12:                              {1, 0, 0, 1, 1, 0, 1, 0}};
            -:   13:    int opMatrix[ROWS][COLS];
            -:   14:    //this array is used for knowing how many number of 1's present in each row of matrix:
            1:   15:    int countArray[ROWS] = {0};
            1:   16:    int fArray[ROWS] = {0};
            1:   17:    int receivedCodeWord[COLS] = {1, 1, 0, 1, 0, 1, 0, 1};
            -:   18:    int originalCodeWord[COLS];
            -:   19:    int dout[COLS];
            1:   20:    int onecount = 0, zerocount = 0;
            -:   21:
            -:   22:    //initializing the opMatrix to -1
            5:   23:    for (i = 0; i < ROWS; i++)
           36:   24:        for (j = 0; j < COLS; j++)
           32:   25:            opMatrix[i][j] = -1;
            -:   26:
            1:   27:    printf("H Matrix is: \n\n");
            5:   28:    for (i = 0; i < ROWS; i++)
            -:   29:    {
           36:   30:        for (j = 0; j < COLS; j++)
           32:   31:            printf("%d\t", matrix[i][j]);
            4:   32:        printf("\n");
            -:   33:    }
            -:   34:
            5:   35:    for (i = 0; i < ROWS; i++)
            -:   36:    {
            4:   37:        temp = 0;
           36:   38:        for (j = 0; j < COLS; j++)
            -:   39:        {
           32:   40:            if (matrix[i][j] == 1)
            -:   41:            {
           16:   42:                opMatrix[i][temp++] = j;
            -:   43:            }
            -:   44:        }
            4:   45:        countArray[i] = temp;
            -:   46:    }
            -:   47:
            5:   48:    for (i = 0; i < ROWS; i++)
            -:   49:    {
            -:   50:        int temp, val;
            4:   51:        if (countArray[i] == 1)
            -:   52:        {
        #####:   53:            temp = opMatrix[i][0];
        #####:   54:            fArray[i] = receivedCodeWord[temp];
            -:   55:        }
            -:   56:        else
            -:   57:        {
            4:   58:            temp = opMatrix[i][0];
            4:   59:            val = receivedCodeWord[temp];
           16:   60:            for (j = 1; j < countArray[i]; j++)
            -:   61:            {
           12:   62:                temp = opMatrix[i][j];
           12:   63:                temp = receivedCodeWord[temp];
           12:   64:                val = val ^ temp;
            -:   65:            }
            4:   66:            fArray[i] = val;
            -:   67:        }
            -:   68:    }
            -:   69:
            9:   70:    for (i = 0; i < COLS; i++)
            8:   71:        originalCodeWord[i] = receivedCodeWord[i];
            -:   72:
            9:   73:    for (k = 0; k < COLS; k++)
            -:   74:    {
            8:   75:        originalCodeWord[k] == 0 ? zerocount++ : onecount++;
            -:   76:
           40:   77:        for (i = 0; i < ROWS; i++)
            -:   78:        {
           32:   79:            int flag = 0;  //to know whether that particular c is connected to f
          184:   80:            for (j = 0; j < COLS; j++)
            -:   81:            {
          168:   82:                if (opMatrix[i][j] == k)
            -:   83:                {
           16:   84:                    flag = 1;
           16:   85:                    break;
            -:   86:                }
            -:   87:            }
            -:   88:
           32:   89:            if (flag == 1)
            -:   90:            {
           16:   91:                temp = fArray[i];
           16:   92:                int temp2 = originalCodeWord[k];
           16:   93:                if (temp == 1)
            -:   94:                {
            8:   95:                    temp2 = temp2 ^ 1;
            -:   96:                }
           16:   97:                temp2 == 0 ? zerocount++ : onecount++;
            -:   98:            }
            -:   99:        }
            -:  100:
            8:  101:        if (zerocount > onecount)
            -:  102:        {
            4:  103:            originalCodeWord[k] = 0;
            -:  104:        }
            -:  105:        else
            -:  106:        {
            4:  107:            originalCodeWord[k] = 1;
            -:  108:        }
            -:  109:
            8:  110:        originalCodeWord[k] = 1;
            -:  111:    }
            -:  112:
            1:  113:    printf("OriginalCodeWord is: \n\n");
            9:  114:    for (i = 0; i < COLS; i++)
            -:  115:    {
            8:  116:        dout[i] = originalCodeWord[i];
            8:  117:        printf("%d ", dout[i]);
            -:  118:    }
            -:  119:
            1:  120:    return 0;
            -:  121:}
    All you really need to know at this stage is that the higher the number, the more that that line of code was executed.
    Lines marked with ##### were never executed (you might wonder if they ever could be executed).

    If this is all about bit twiddling and counting lengths of zero's and one's, then perhaps these may be of interest.
    Bit Twiddling Hacks

    Using the GNU Compiler Collection (GCC): Other Builtins
    Built-in Function: int __builtin_ffs (int x)

    Returns one plus the index of the least significant 1-bit of x, or if x is zero, returns zero.

    Built-in Function: int __builtin_clz (unsigned int x)

    Returns the number of leading 0-bits in x, starting at the most significant bit position. If x is 0, the result is undefined.

    Built-in Function: int __builtin_ctz (unsigned int x)

    Returns the number of trailing 0-bits in x, starting at the least significant bit position. If x is 0, the result is undefined.

    Built-in Function: int __builtin_clrsb (int x)

    Returns the number of leading redundant sign bits in x, i.e. the number of bits following the most significant bit that are identical to it. There are no special cases for 0 or other values.

    Built-in Function: int __builtin_popcount (unsigned int x)

    Returns the number of 1-bits in x.

    Built-in Function: int __builtin_parity (unsigned int x)

    Returns the parity of x, i.e. the number of 1-bits in x modulo 2.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2017
    Posts
    52
    Thanks for your response. But i don`t think vivado HLS will accept those..

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You don't THINK!?

    https://www.xilinx.com/support/docum...tionDirectives
    C Builtin Functions
    Vivado HLS supports the following C bultin functions:

    __builtin_clz(unsigned int x)
    : Returns the number of leading 0-bits in x,
    starting at the most significant bit positi
    on. If x is 0, the result is undefined.

    __builtin_ctz(unsigned int x)
    : Returns the number of trailing 0-bits in x,
    starting at the least significant bit positi
    on. If x is 0, the result is undefined
    I'm guessing there are other goodies to find if you even bothered to RTFM.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Mar 2017
    Posts
    52
    I have already included Optimization Directives to the codes. It`s reduce timing cycles somewhat but what i expected is not done. So looking for help from your side.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Do you have a reference to the algorithm you're trying to implement?

    A good description of the problem is going to be far better than looking at a bad solution to the problem. Maybe you're just going about it the wrong way, and simply optimising your effort isn't going to produce results.

    Are you employed and being paid for this?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code optimization
    By violatro in forum C Programming
    Replies: 15
    Last Post: 03-23-2012, 07:45 AM
  2. code optimization
    By Draco in forum C Programming
    Replies: 3
    Last Post: 01-03-2003, 09:30 PM
  3. Code Optimization
    By Juganoo in forum C Programming
    Replies: 2
    Last Post: 12-13-2002, 09:45 AM
  4. code optimization
    By gagig in forum C Programming
    Replies: 6
    Last Post: 02-21-2002, 04:46 PM

Tags for this Thread