### Thread: How to optimize multiple FOR loops in matrix operation in c?

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2017
Posts
17
Rep Power
0

#### How to optimize multiple FOR loops in matrix operation in c?

I have written C codes with multiple FOR LOOPS for implementation of a FPGA . How to optimize the C codes.Please help me
Code:
```#define ROWS 102
#define COLS 204
int main()
{

int i,j,sum=0,k,r2,j2,i2;

int r,c,n;
int temp=0;

k, r= ROWS;

c, n= COLS;
///
for(i=0;i<n-k;i++)
{
j=n-k+i;
if(H[i][j] != 1)
{
for(i2=i+1;i2<r;i2++)
{
if(H[i2][j] == 1)
{
for(j2=0;j2<c;j2++)
{
temp = H[i2][j2];
H[i2][j2] = H[i][j2];
H[i][j2] = temp;
}
break;
}

}
}
for(i2 = 0;i2<r;i2++)
{
if(i2 != i && H[i2][j] == 1)
{
for(j2=0;j2<c;j2++)
{
H[i2][j2] = abs(H[i][j2] - H[i2][j2]);
}
}
}
}

for(i=0;i<ROWS;i++)
{
for(j=0;j<COLS;j++)
printf("%d",H[i][j]);
}

int G[ROWS][COLS] = {0};
for (i=0;i<k;i++)
for(j=0;j<k;j++)
if(i == j)
G[i][j] = 1;

for(i=0;i<r;i++)
for(j=0;j<k;j++)
G[j][k+i] = H[i][j];

for(i=0;i<ROWS;i++)
{
for(j=0;j<COLS;j++)
printf("%d",G[i][j]);
}
int C[ROWS];
int s = 0;
for(j=0;j<n;j++)
{
for(i=0;i<k;i++)
{
s = s+msg[i]*G[i][j];
}
C[j] = s%2;
s = 0;
}
for(i=0;i<n;i++)
printf("%d",C[i]);
}```
2. Since this source won't compile, forgo the fancy gate array. A simple NOT circuit suffices. Major errors are shown at the end of this version of your program.
Code:
```#include<stdio.h>

#define ROWS 102
#define COLS 204
int main() {
int i,j,k,j2,i2;

int r,c,n;
int temp=0;

k, r= ROWS;

c, n= COLS;
///
for(i=0;i<n-k;i++) {
j=n-k+i;
if(H[i][j] != 1) {
for(i2=i+1;i2<r;i2++) {
if(H[i2][j] == 1) {
for(j2=0;j2<c;j2++) {
temp = H[i2][j2];
H[i2][j2] = H[i][j2];
H[i][j2] = temp;
}
break;
}
}
}
for(i2 = 0;i2<r;i2++) {
if(i2 != i && H[i2][j] == 1) {
for(j2=0;j2<c;j2++) {
H[i2][j2] = abs(H[i][j2] - H[i2][j2]);
}
}
}
}

for(i=0;i<ROWS;i++) {
for(j=0;j<COLS;j++)
printf("%d",H[i][j]);
}

int G[ROWS][COLS] = {0};
for (i=0;i<k;i++)
for(j=0;j<k;j++)
if(i == j)
G[i][j] = 1;

for(i=0;i<r;i++)
for(j=0;j<k;j++)
G[j][k+i] = H[i][j];

for(i=0;i<ROWS;i++) {
for(j=0;j<COLS;j++)
printf("%d",G[i][j]);
}
int C[ROWS];
int s = 0;
for(j=0;j<n;j++) {
for(i=0;i<k;i++) {
s = s+msg[i]*G[i][j];
}
C[j] = s%2;
s = 0;
}
for(i=0;i<n;i++)
printf("%d",C[i]);
}
#if 0
-*- mode: compilation; default-directory: "/tmp/" -*-
Compilation started at Fri Oct  6 08:52:52

a=./c && make \$a && \$a
cc -Wall -g -fPIC -fopenmp -I/home/lambertdw/include -I/usr/local/include   c.c -lgomp  -o c
c.c: In function ‘main’:
c.c:11:4: warning: left-hand operand of comma expression has no effect [-Wunused-value]
k, r= ROWS;
^
c.c:13:4: warning: left-hand operand of comma expression has no effect [-Wunused-value]
c, n= COLS;
^
c.c:17:8: error: ‘H’ undeclared (first use in this function)
if(H[i][j] != 1) {
^
c.c:17:8: note: each undeclared identifier is reported only once for each function it appears in
c.c:32:16: warning: implicit declaration of function ‘abs’ [-Wimplicit-function-declaration]
H[i2][j2] = abs(H[i][j2] - H[i2][j2]);
^~~
c.c:61:13: error: ‘msg’ undeclared (first use in this function)
s = s+msg[i]*G[i][j];
^~~
<builtin>: recipe for target 'c' failed
make: *** [c] Error 1

Compilation exited abnormally with code 2 at Fri Oct  6 08:52:52
#endif```
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2017
Posts
17
Rep Power
0
No, my code will compile. if you declare H matrix and message. I am unable to declare H matrix variable in this post. Please make use of the link. In that link i have attached source codes.
H matrix size 102x204
message =102

Code:
`int msg[ROWS] = {0,1,0,0,0,1,1,1,0,0,0,0,1,1,0,1,0,0,1,0,0,0,0,1,0,0,1,0,1,0,0,0,1,0,1,1,0,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,1,0,1,0,0,0,0,1,0,1,1,0,0,0,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,1,1,0,0,1,1,1,0,0,1};`
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2017
Posts
17
Rep Power
0
5. #### Is this your program?

This version of your program compiles by gcc and runs. Does it do as you expect? What about the program do you need optimized? Please read these changes and questions.
I've declared static int msg[ROWS]; thereby initialized as 0. Changed your comma operators to
k = r = ROWS;
c = n = COLS;
put H into the program such that if fits into devshed format. Note that H has ones along 2 diagonals. Was this program translated from a FORTRAN program? Are ROWS and COLS swapped? H either looks like
Code:
`\ \`
or
Code:
```\
\```
with noise. I inserted a newline character when printing H. I changed `abs' to `ABS' including a macro definition. Otherwise, I believe I've just changed the indentation.
Code:
```#include<stdio.h>
#include<math.h>

#define ROWS 102
#define COLS 204

#define ABS(A) ((A) < 0 ? -(A) : (A))

static int H[ROWS][COLS];
static int msg[ROWS];

static char*ache=
"100000000000000001000000001000000000000000000000000000000000000000000000000000000100000000000000000001100000000000000000000000000100000000000000000000000000000000000000000000001000000000000100000000000010"
"010000000000001000000000000000000000000000000000000000000000000000000000001000010000010000000000000000010000000000000000100000000000000000001001000000000000000000000000000000000000000000000010000000000000"
"001000000000000000000000000000110000000001000000000000000000000000000000000000000000000000000000000010001000000000000000000000000000000000000000000000001000001000000000000000000100000000000000000000100000"
"000100000000000000000000000000000000100000000000000000000000000000000010001000000000100000000000000000000100001001000000000000100000000000000000000000000000000000010000000000000000000000000000000000000000"
"000010000000000010000000000000000000000001000000000000000000000000000001000000000000001000000000000000000010000000000000000000000000000000001000000000000000010000000000000000000000000000000000100010000000"
"000101000000000010000000000000000000000000000000000000000000000000000000000001000000000000000000000001000001000000000000000000000000000000000000000000000000000001001010000001000000000000000000000000000000"
"000000100000000000000010000000000000000000000000000000000000000000000000100000010010000000000000000000000000100000000100000000000000000000000000000000000000000000000001000000001100000000000000000000000000"
"000000010000000000000000000000001000100000000000000000000000000000000000100000000000000000000000000100000000010000000000000000000000000000000000000000000001000000000000000010000000000000000001000010000000"
"000000001000000000000000000000000000000110000000000000000000000000000000000000000000000100010000000000100000001000000000000000000000000000000000000000000000000000000000000010000000100000000000000000100000"
"000000000100010000000010000000000000000000000000000010000000000000000000000000000000000000001000000000000010000100000000000000000000000010000000000000000000000000000010000000100000000000000000000000000000"
"000000000010000000100000010100000000000000000000000000000000000000000000000000000000000100000000000000000000010010010000000000000000000000000000000000000000000000000000010000000000000000000010000000000000"
"100000000001000000000000100000000000000000000000000000000000000000000000000000000000010000000000100000000001000001000000000100000000000000000000000000000000000010000000000000000000000000000001000000000000"
"000100000000100000000000000000001100000000000000010000000000000000000000000000000000000000000000000000000000000000100000000000000000000010010000010000000000000100000000000000000000000000000000000000000000"
"000000000000010000000000000000100000000100000000000000000100000000000000000000000000000000000010000000000000000000010000100000000000000000000000000000000000000000000100000001000000000000000000000000000010"
"000000000000001000000000000000000000000000000000000000000000000000010100000000000000001000000000000010000000100010001000000000000000000000000000000000000000000000001000000000000001000000000000000000000000"
"000100000000000100000000000000000000000000000000000000000010000000100000000000000000000000010000000000000000000000000100000010000010000001000000000000100000000000000000000000000000000000000000000000000000"
"000000000000001010000000000000000000000000010000000000000000000000000010100000000000000000000000000000000000000000000010000000000000000000000000000000000000000010000000000000000000000000000100010001000000"
"000000000000000001000000000000000000000000000010000000000000010000000000000000000000100000001000000000000000000000000001010010000000000000000000010000000000000000000000000000000000100000000000000000000000"
"000000000000000000100110000000000000100000000000000000001000000000000000000000000000000000000000000000000000000000000000100001000110000000000010000000000000000000000000000000000000000000000000000000000000"
"100000000000000000010000000000000000000000000000011000000000000000000000000000000000000000000000001000000100000000000000010000000000000000000000000000000000000000000000100000000010000000000000000000010000"
"000000000000000000001000000000010000000000000000000000000000010000000001000000000000000000000000010000000000000000000000001001000000000000000000000000000100000000000001000000000000000000000000000000000001"
"000000000000000000000100000000100000000000010000000000000000000000000000000000000000000010000000010000000000000000000000000100000000000000001000000000000000000100000000100000010000000000000000000000000000"
"000000000000000000000010000000010000000000000000000000000000100010000000000000000000000000000000001000000000000000000000000010000001100000000000001000010000000000000000000000000000000000000000000000000000"
"000000000000000100000001000000000000000001000000000000000000000000000000100010000000000000000000000000000000000000000000000001000000000000000100111000000000000000000000000000000000000000000000000000000000"
"000010000000000000000001100000000000000000000000000000100000000000100000000000000000000000000000000000000000000000000000000000100000100000000000000000000000000000001000000000000100000010000000000000000000"
"000000000100000000000000011000000000000100000000000000000000000000000000000000000000100000000000000000000000000000000000000000010000000000000000001000000010000100000000000000000000000000000000000000010000"
"000000000000000000000100001000000000000001000000010000000000000000000000010000000000000000000000000000000001000000000000000000001000000000000000000000000100000000000000010000000000000000000000001000000000"
"010000000000000000000000000100000000000000000000000000000000001000100000000000000000000000000100000000000000000000000000000000000100000000000000000001000000000000000000001000000000000000100001000000000000"
"000000000000000000000000000010000000000000000001000000000000000000001000000000100000000001000000000000000000000000010000000000000010000000000000000000000000001000000000000000010000000000000000000000010000"
"000000100000000000000000000001000000000000000000000010000100000000000000000000000000000000000100000000000000000000000000000000100001000000010000000000000000000001000000000000000000000000001000000000000000"
"000000000000000000000000000010100000000000000000000000000000000100000000000010000000000000000100000000000000000000001000000000000000100000000000000000000000000000000010000000000000001000000100000000000000"
"000000000000000000010000000001010000000000000000000000000000000000000000010001000000000000000000000000000000000000100000000000000000010000000000000000100000000000000000000000000000100000000000010000000000"
"000000000010000000000000000000001000000000000010000000000000000000000000000100001000000000000000000000000000100000000000000000000000001000000000000000000110000000000000000000000010000000000000000000000000"
"010000100000000000000000000000000100000000000000000000000000000000000010000100000000000000000000000000000000000000000000000000000000000101000000000000000000001000000000000000000000011000000000000000000000"
"000000000000000000000000001010000010000000000000000000000001000000000000000000000000010000000000000000000000000000000000001000000000000010000000000010000000000000000100000000000000000000001000000000000000"
"000000000000000010000000000000000011000000000001000000000000000000000000000000000000000000000000000100000000000000000000000000000000000001100000010000000000000000100001000000000000000000000000000000000000"
"000010000000000000000000000000000100110000000000000000000000000000001000000000000000000000000000000000000000000000001000000000000000000000100000100000000000000000000000100000000000000000000000000000000010"
"000000000000000000000000000000000000010000000000000000010000000000000000000100000000000000010000000010000100000000000000001000000000000000010000000000000000000010000000010000000000000000000000000000000000"
"000000000000000000000000000000000000001010000000000000000001000000000000000101000000000000000000000000000000000000000000000010000000000000001100000000000000000000000000000000100000000000000000000001000000"
"010000000000000000000000000000000010000100100000000000000000000000000000000000000000000000001000000000000000000001000000000000000000001000000100000100000000010000000000000000000000000000000000000000000000"
"110000000000100000000000000000000000000010000000000000000010000000000000000000000000000000000000000000000000100000000010000000000001000000000010000000000000100000000000000000000000000000000000000000000000"
"000000000010000000000000000000000000000001100000000000000000000000000000000000000000000000010010000000000000000000101000000000000000000000000001000000000001000000000000000000000000000000000000000000000001"
"000000101000000000000000000000000000000000101000000000010000000000000000000000000000000000000000000000000000000000000000010000000000000000000000100000000000000000000000000000000000000000000001010000001000"
"000000000100000000000000000000000000000000010000000000000000000000000000001000000010000000000010000000000000010000000000000000000000001000000000010000000000100000000000000000000001000000000000000000000000"
"000000000000000000000000000000000000000000001000000000000000001000000000000000101000000000000000100000000000000000000000000000100000000000100000001100000000000000000000000000000000000000000000001000000000"
"000000000000000000000000000000000100000000000100000000000000100001000000000010000000000000000000000000001000000000000000000000000000000000000001000100000000000000000000000000000000000000000000000010001000"
"000000000000010000010000000000000000000000000110000000000000000000000000000000000010000000000000000000000000000000000000000100010001000000000000000010000000001000000000000000000000000000000000000000000000"
"000000010000100000000000010000000000000000000001000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000011000000000000000000000001000000000000000000101000000000"
"000000000000001000001000100000000000000000000000101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100010100000000000000000000000001000000000010000000000000000"
"000000010000000000000000000000000000000000000000110001000000000000000000000000000000000010000000000000001000000000000000000001000000000000000000000000010000000000011000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000100000001000000000000001010000000000000000000000100000000000000000000000000000000000000010100000000000000000001000000000000000000000000000000000101000000000000000"
"100000000000000000000000000000000000001000001000000101000000000000000000000000000000000000000000000000000000010000000000000000000000000000010000000001000100000000100000000000000000000000000000000000000000"
"000000010000000000000000000000000000000000000000000010000001000100000000000000000000000001000000000000000000000000000000000000000000000001000000000000000010100000000000000000000000000000000000010000000001"
"000000000001000000000000000100000000000000000000000001000001000000000000000000000100000000000000000000000000000100000000000000000000000000100000000000100001000000000000000000000000001000000000000000000000"
"001000000000000010000000000000000000100000000000000000100000000000000000000000000000000000000000001000000000000100000000000000000000000000000000000100000000100000000000000100000000000000001000000000000000"
"000000000000000000010000000000100000000000000000000000010000000000000000000000010000000000100000000000000000000000000000000000000000000000000000000000010001010001000000000000000001000000000000000000000000"
"000000000000000000000000000100000000000000000000000000001000000001000000000000001000000010000000000000000000000000000000000000000000010010000000000000000000001000000000000100000000000000000000000100000000"
"000010000000000000000000000000000000000000000000000000000100000000000100000001000000000000100000000000000000000000000100000000000000000000000000000010000100000100000000000000000000000000000000000000001000"
"000000000000000001000001000000000000000000000000000000000010000000000000000000000000000100000001000000000000000000000000000000000000000000000000000001010000000010000000000000000010001000000000000000000000"
"000000000000000000001000000000000000010000000000000000100011000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000010000000000100100000010000000"
"000000000000000000000000000000000000000000000000000000000000100000001000010000000000000000001000000010000000001000000000000000010000000000000000000000000000000000100000000000000000000000000000100100000000"
"000001100000000000000000000000000010000000000000000000000000010000000000000000000000000000000001000000100000000010000000000000001000000000000000000000000000000000010000000000000000000000000000000000010000"
"000000000000000100000000001000000000000000010000000000000000001000000000000000000000000100000000000000000000000000000000100000000000010000000000000000000000000000001000000000100000000000010000000000000000"
"000100000000000000000000010000000000000000000000000000000000000100000000000000100001000000000000000000000000000000000010000000000000010100000000000000000000000000000101000000000000000000000000000000000000"
"000000000001100000000000000000000000000000000000000100001000000010000000000000000000000000000000000000000100000000000000000000000000000001000100000000000000000000000010000000010000000000000000000000000000"
"000000000000000000000001000001000000000000000000000100000000000001000000000000000000000000000000000001000000000100001000010000000000001000000000000000000000000000000001000000000000000000000000000000000000"
"000001000000000000000000000000000000000000000000000000001000000000100000000000000000100000000000100000000000000000000000000000000000000000010000000000000000000000000000100000000000010101000000000000000000"
"000001000000000000000000000000000000000000000000000000000100000000010010000000000000000001000000000000000000000000000000000000000000010000000010100000010000000000000000010000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000000001001010000000001000000010000000000000000000000000110000000001000000000000000000000100000000000000000000000000000000001000000000000000000000000000000000"
"000000000000000000100001000000000000000000000000000000010000000000000100000000000000000000000000000100001000000001000000000000011000000000000000000000000000000000000000000100000000000000000000000000000000"
"000000000000010000000000000000000000000000000000000100000000001000000110000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000100010000100000000000000000100000000"
"000000000010000000000000100000000000001000000000000000000000000001000001000000000000000000000000000000000000000000000001000000000101000000000000000000000000000000000000000001000000000000000000000000100000"
"000000000000000000000000000000000000000000000001100000000000000100000000100000000000000000000000010000000000001000000000000000000000000000000000000000000000000000000000000000100010000000000000000000001100"
"000000001000001000000000000000000000000000000000000000000100000000000000010000000001000000000000000000000010000000000001000000000000001000000000000000000000000000010000000000010000000000000000000000000000"
"000000000000000000000000000100000000000010000000000000000000000000000100001000000000000000000001000000000000000000000000000000000000100000000000000000000010000000000100000000001000000000000000000000000100"
"000000000000000000000000010000000000000000001000000000000000000010000000000100000000000000000100000000000000000000000000000000000000000000000001000000000000010000000000000100000100000000000000000000000010"
"000000000100100000000000000000000000000000000000000000000000000000000000000010000001001000000000000000000000000000000000000000100000000000000000000000100000000000100000000000000010000000000010000000000000"
"000000000000000000000000000000000000000000000000100000100000000010000000000001000000000001000000000000000000000000000000010000000100000000100000000000000000000000000000000000000001010000000000000000000000"
"000000000000000000000000000000001000000000100000000000000000000000100000000000100000000000000000001000000000000000000000001000000000000000001000000000000000000000000000000000000000100000010000000000000100"
"000000000000000001100000000000000010000000000100000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000001000000000000010000000000000010000010000100000000000"
"000000001000000100000000000000000001000000000000000000100000000000000000000000001000000000000000000000000000000000000000000000000000000000000010000000000000000100000000000000000000001001000010000000000000"
"000000000110000000000000000000000000000000000000000000000000000000000000000000100100000000000001000000000001001000000000000001000000000000000000000000000000000000000000000100000000000100000000000000000000"
"000000000001000000000000000000000001000000000000000000000000000000001000000000000010001000000000000000000000000000000000000000000000000000000000000100000000000000000000000001000000000010000000000000000101"
"000000000000000000000000000000000000000000001011000000000000000000000000001000000001000000000000000000000001000000000000001000000000000000000100000000001000000000000000000000000000000001000000000000000000"
"000000001000000000000000000000000000000000000100000011000000000000000000000000000000100000000000000000000000000000100000100000001000000000000000000000000000000000000000000000001000000000100000000000000000"
"000000000000000000000100000000000000000000000000000000000000000100000000000000000010010000100000000000000010000000010000000000000000000000000000000000000000000000000000001000000000000000010000000000100000"
"000000010000000000000100000000000000000000000000000000000000100000000000000000001000001000000000000000010000000000000001000000000000000000000000000000000000000000000000000000000000010000001000000001000000"
"000000000000000000000000000000011000001000000000001000000000000000000000000000000000000100000000000000000000000000000000000000001000000000000010000000000000000000000000001000000001000000000100000000000000"
"001000000000000000001000000000000000000000000000000100000000000000000000000000000000000010000000000100100000100000100000000010000000000000000000000000000000000000000000000000000000000000000010000000000000"
"000000000000000000010000000000000000000100010000000000000000100000000000000000000000000001000000000000000000000000000100000000000000000010000000000000001000000000000000000000000000000100000001000000000000"
"000001000000000000000010000010000000000000000000000000000000000000000000000000000001000000100000000000000000000000000000000000000000000000000001000000000010000000000000000010000000000010000000100000000000"
"000000000000010000000000000000000000000000000000100000000000000000000001000000000000000000010000000001000000000000000000000000000000000000000000001000000000000000000000001000000000000011000000010000000000"
"000010000000000000001000000010000000000000000000000000000000000010000000000000000000000000001000000000001000010000000010000000000000000000000000000000000000000000000000000000000000000100000000001000000000"
"000000000000000000000000000000000000001000000000000000000000000000000000000000010100000000000100010000000010000010000000000000000010000000000000000000000000000000000100000000000000000000000000000100000000"
"000000000000000000000000000001000000000000000010010000000000001000000000000000000000000000000010000000010000000000000100000000000000000000000000000000000000000010100000000000000000000000000000000010000000"
"000000000000000000000000100000000100000000000000000000010000000000010000000000000000000000000001000000000000000000010000000000000000000000000000000000000000000000000000000000000000100001000000000101000000"
"000000000000000000000000000000000001010000000100000000000000000000000000000000000000000010000000100000000000000010000000000000000000000100000000000001000000100000000000000000000000000000000000000000100000"
"001000000001000001000000000001000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000100000000000000000000000000000000010000000000000000100000000001010000"
"000000000000000000000000000000000000000000000000000010000000010000000000010000000000000000000000101000010000000000000010000000000000000000000000000000000001000000000000000000000000000010000000000000001000"
"000000000000000100000000000000000000000000000000000000000000000000010000000000000100000000000010000100000100000000000000000100000000000000000000000000000000010000000000000000000000000000000000001000000100"
"000000000000000000100000000000000001000010000000000000000000000000000001000000000000000000000000000010000000000100000000000100000000000000000000000000000000000001010000000000000000000000000000000000000010"
"001000000000000000000000000000000000010000000000001000000000000000000000000000000000010000000000000001000000000000000000000000010000000000000000000000000000000000000000000010100000000100000000000000000001"
;

int main() {
int i,j,k,j2,i2;

int r,c,n;
int temp=0;

/* construct H */
int*pH = H[0];

for (i = 0; i < ROWS*COLS; ++i)
pH[i] = '1' == ache[i];

k = r = ROWS;
c = n = COLS;

for(i=0;i<n-k;i++) {
j=n-k+i;
if(H[i][j] != 1) {
for(i2=i+1;i2<r;i2++) {
if(H[i2][j] == 1) {
for(j2=0;j2<c;j2++) {
temp = H[i2][j2];
H[i2][j2] = H[i][j2];
H[i][j2] = temp;
}
break;
}
}
}
for(i2 = 0;i2<r;i2++) {
if(i2 != i && H[i2][j] == 1) {
for(j2=0;j2<c;j2++) {
H[i2][j2] = ABS(H[i][j2] - H[i2][j2]);
}
}
}
}

for(i=0;i<ROWS;i++, putchar('\n')) { /* Inserted putchar('\n') */
for(j=0;j<COLS;j++)
printf("%d",H[i][j]);
}

int G[ROWS][COLS] = {0};
for (i=0;i<k;i++)
for(j=0;j<k;j++)
if(i == j)
G[i][j] = 1;

for(i=0;i<r;i++)
for(j=0;j<k;j++)
G[j][k+i] = H[i][j];

for(i=0;i<ROWS;i++) {
for(j=0;j<COLS;j++)
printf("%d",G[i][j]);
}
int C[ROWS];
int s = 0;
for(j=0;j<n;j++) {
for(i=0;i<k;i++) {
s = s+msg[i]*G[i][j];
}
C[j] = s%2;
s = 0;
}
for(i=0;i<n;i++)
printf("%d",C[i]);
}```
Last edited by b49P23TIvg; October 9th, 2017 at 12:34 PM. Reason: insert title
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2017
Posts
17
Rep Power
0

In my codes, i have used lot of FOR LOOPS. I need to optimize.
7. First step in optimization is: 1) Make a working program.

Next profile the code to find out where your efforts will have greatest impact and work there. I didn't do this step.

Make sure that changes to the program don't make the program incorrect. I did this, it is the `cmp got expect' part of the command line.

Time the program in expected use cases. I have only one hard coded case to try.

Since the program seems to work with single bits it may be possible to rewrite to pack the data into words and use bit operations. Memory would be reduced by roughly a factor of the word length, and some of the operations would run much faster, because they'd take place in parallel. As is the program is mostly pounding the least significant bit.

The gcc -O2 optimization greatly reduces the run time of the program. Does your compiler have these optimizations? https://gcc.gnu.org/onlinedocs/gcc-7...timize-Options

I've inserted comments where I made changes. First thing I looked for was that the outer loop should be small relative to the inner loop count. This reduces the number of times the inner loop must be set up. Seems you've done that. I changed the data type to short int. Replaced multiplication, division, and absolute value with bitwise operations. Combined loops. Removed an inner loop entirely.

Do you have an optimizing compiler? If not, you'll need to unroll loops and other tricks by hand.
Code:
```/*
a=./c && make CFLAGS=-O4 \$a && \$a> got && cmp got expect && time \$a > /dev/null

(  cc -O4 -Wall c.c -o c  )

*/

#include<stdio.h>
#include<math.h>

#define ROWS 102
#define COLS 204

static short int H[ROWS][COLS];
static int msg[ROWS];

static char*ache=
"100000000000000001000000001000000000000000000000000000000000000000000000000000000100000000000000000001100000000000000000000000000100000000000000000000000000000000000000000000001000000000000100000000000010"
"010000000000001000000000000000000000000000000000000000000000000000000000001000010000010000000000000000010000000000000000100000000000000000001001000000000000000000000000000000000000000000000010000000000000"
"001000000000000000000000000000110000000001000000000000000000000000000000000000000000000000000000000010001000000000000000000000000000000000000000000000001000001000000000000000000100000000000000000000100000"
"000100000000000000000000000000000000100000000000000000000000000000000010001000000000100000000000000000000100001001000000000000100000000000000000000000000000000000010000000000000000000000000000000000000000"
"000010000000000010000000000000000000000001000000000000000000000000000001000000000000001000000000000000000010000000000000000000000000000000001000000000000000010000000000000000000000000000000000100010000000"
"000101000000000010000000000000000000000000000000000000000000000000000000000001000000000000000000000001000001000000000000000000000000000000000000000000000000000001001010000001000000000000000000000000000000"
"000000100000000000000010000000000000000000000000000000000000000000000000100000010010000000000000000000000000100000000100000000000000000000000000000000000000000000000001000000001100000000000000000000000000"
"000000010000000000000000000000001000100000000000000000000000000000000000100000000000000000000000000100000000010000000000000000000000000000000000000000000001000000000000000010000000000000000001000010000000"
"000000001000000000000000000000000000000110000000000000000000000000000000000000000000000100010000000000100000001000000000000000000000000000000000000000000000000000000000000010000000100000000000000000100000"
"000000000100010000000010000000000000000000000000000010000000000000000000000000000000000000001000000000000010000100000000000000000000000010000000000000000000000000000010000000100000000000000000000000000000"
"000000000010000000100000010100000000000000000000000000000000000000000000000000000000000100000000000000000000010010010000000000000000000000000000000000000000000000000000010000000000000000000010000000000000"
"100000000001000000000000100000000000000000000000000000000000000000000000000000000000010000000000100000000001000001000000000100000000000000000000000000000000000010000000000000000000000000000001000000000000"
"000100000000100000000000000000001100000000000000010000000000000000000000000000000000000000000000000000000000000000100000000000000000000010010000010000000000000100000000000000000000000000000000000000000000"
"000000000000010000000000000000100000000100000000000000000100000000000000000000000000000000000010000000000000000000010000100000000000000000000000000000000000000000000100000001000000000000000000000000000010"
"000000000000001000000000000000000000000000000000000000000000000000010100000000000000001000000000000010000000100010001000000000000000000000000000000000000000000000001000000000000001000000000000000000000000"
"000100000000000100000000000000000000000000000000000000000010000000100000000000000000000000010000000000000000000000000100000010000010000001000000000000100000000000000000000000000000000000000000000000000000"
"000000000000001010000000000000000000000000010000000000000000000000000010100000000000000000000000000000000000000000000010000000000000000000000000000000000000000010000000000000000000000000000100010001000000"
"000000000000000001000000000000000000000000000010000000000000010000000000000000000000100000001000000000000000000000000001010010000000000000000000010000000000000000000000000000000000100000000000000000000000"
"000000000000000000100110000000000000100000000000000000001000000000000000000000000000000000000000000000000000000000000000100001000110000000000010000000000000000000000000000000000000000000000000000000000000"
"100000000000000000010000000000000000000000000000011000000000000000000000000000000000000000000000001000000100000000000000010000000000000000000000000000000000000000000000100000000010000000000000000000010000"
"000000000000000000001000000000010000000000000000000000000000010000000001000000000000000000000000010000000000000000000000001001000000000000000000000000000100000000000001000000000000000000000000000000000001"
"000000000000000000000100000000100000000000010000000000000000000000000000000000000000000010000000010000000000000000000000000100000000000000001000000000000000000100000000100000010000000000000000000000000000"
"000000000000000000000010000000010000000000000000000000000000100010000000000000000000000000000000001000000000000000000000000010000001100000000000001000010000000000000000000000000000000000000000000000000000"
"000000000000000100000001000000000000000001000000000000000000000000000000100010000000000000000000000000000000000000000000000001000000000000000100111000000000000000000000000000000000000000000000000000000000"
"000010000000000000000001100000000000000000000000000000100000000000100000000000000000000000000000000000000000000000000000000000100000100000000000000000000000000000001000000000000100000010000000000000000000"
"000000000100000000000000011000000000000100000000000000000000000000000000000000000000100000000000000000000000000000000000000000010000000000000000001000000010000100000000000000000000000000000000000000010000"
"000000000000000000000100001000000000000001000000010000000000000000000000010000000000000000000000000000000001000000000000000000001000000000000000000000000100000000000000010000000000000000000000001000000000"
"010000000000000000000000000100000000000000000000000000000000001000100000000000000000000000000100000000000000000000000000000000000100000000000000000001000000000000000000001000000000000000100001000000000000"
"000000000000000000000000000010000000000000000001000000000000000000001000000000100000000001000000000000000000000000010000000000000010000000000000000000000000001000000000000000010000000000000000000000010000"
"000000100000000000000000000001000000000000000000000010000100000000000000000000000000000000000100000000000000000000000000000000100001000000010000000000000000000001000000000000000000000000001000000000000000"
"000000000000000000000000000010100000000000000000000000000000000100000000000010000000000000000100000000000000000000001000000000000000100000000000000000000000000000000010000000000000001000000100000000000000"
"000000000000000000010000000001010000000000000000000000000000000000000000010001000000000000000000000000000000000000100000000000000000010000000000000000100000000000000000000000000000100000000000010000000000"
"000000000010000000000000000000001000000000000010000000000000000000000000000100001000000000000000000000000000100000000000000000000000001000000000000000000110000000000000000000000010000000000000000000000000"
"010000100000000000000000000000000100000000000000000000000000000000000010000100000000000000000000000000000000000000000000000000000000000101000000000000000000001000000000000000000000011000000000000000000000"
"000000000000000000000000001010000010000000000000000000000001000000000000000000000000010000000000000000000000000000000000001000000000000010000000000010000000000000000100000000000000000000001000000000000000"
"000000000000000010000000000000000011000000000001000000000000000000000000000000000000000000000000000100000000000000000000000000000000000001100000010000000000000000100001000000000000000000000000000000000000"
"000010000000000000000000000000000100110000000000000000000000000000001000000000000000000000000000000000000000000000001000000000000000000000100000100000000000000000000000100000000000000000000000000000000010"
"000000000000000000000000000000000000010000000000000000010000000000000000000100000000000000010000000010000100000000000000001000000000000000010000000000000000000010000000010000000000000000000000000000000000"
"000000000000000000000000000000000000001010000000000000000001000000000000000101000000000000000000000000000000000000000000000010000000000000001100000000000000000000000000000000100000000000000000000001000000"
"010000000000000000000000000000000010000100100000000000000000000000000000000000000000000000001000000000000000000001000000000000000000001000000100000100000000010000000000000000000000000000000000000000000000"
"110000000000100000000000000000000000000010000000000000000010000000000000000000000000000000000000000000000000100000000010000000000001000000000010000000000000100000000000000000000000000000000000000000000000"
"000000000010000000000000000000000000000001100000000000000000000000000000000000000000000000010010000000000000000000101000000000000000000000000001000000000001000000000000000000000000000000000000000000000001"
"000000101000000000000000000000000000000000101000000000010000000000000000000000000000000000000000000000000000000000000000010000000000000000000000100000000000000000000000000000000000000000000001010000001000"
"000000000100000000000000000000000000000000010000000000000000000000000000001000000010000000000010000000000000010000000000000000000000001000000000010000000000100000000000000000000001000000000000000000000000"
"000000000000000000000000000000000000000000001000000000000000001000000000000000101000000000000000100000000000000000000000000000100000000000100000001100000000000000000000000000000000000000000000001000000000"
"000000000000000000000000000000000100000000000100000000000000100001000000000010000000000000000000000000001000000000000000000000000000000000000001000100000000000000000000000000000000000000000000000010001000"
"000000000000010000010000000000000000000000000110000000000000000000000000000000000010000000000000000000000000000000000000000100010001000000000000000010000000001000000000000000000000000000000000000000000000"
"000000010000100000000000010000000000000000000001000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000011000000000000000000000001000000000000000000101000000000"
"000000000000001000001000100000000000000000000000101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100010100000000000000000000000001000000000010000000000000000"
"000000010000000000000000000000000000000000000000110001000000000000000000000000000000000010000000000000001000000000000000000001000000000000000000000000010000000000011000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000100000001000000000000001010000000000000000000000100000000000000000000000000000000000000010100000000000000000001000000000000000000000000000000000101000000000000000"
"100000000000000000000000000000000000001000001000000101000000000000000000000000000000000000000000000000000000010000000000000000000000000000010000000001000100000000100000000000000000000000000000000000000000"
"000000010000000000000000000000000000000000000000000010000001000100000000000000000000000001000000000000000000000000000000000000000000000001000000000000000010100000000000000000000000000000000000010000000001"
"000000000001000000000000000100000000000000000000000001000001000000000000000000000100000000000000000000000000000100000000000000000000000000100000000000100001000000000000000000000000001000000000000000000000"
"001000000000000010000000000000000000100000000000000000100000000000000000000000000000000000000000001000000000000100000000000000000000000000000000000100000000100000000000000100000000000000001000000000000000"
"000000000000000000010000000000100000000000000000000000010000000000000000000000010000000000100000000000000000000000000000000000000000000000000000000000010001010001000000000000000001000000000000000000000000"
"000000000000000000000000000100000000000000000000000000001000000001000000000000001000000010000000000000000000000000000000000000000000010010000000000000000000001000000000000100000000000000000000000100000000"
"000010000000000000000000000000000000000000000000000000000100000000000100000001000000000000100000000000000000000000000100000000000000000000000000000010000100000100000000000000000000000000000000000000001000"
"000000000000000001000001000000000000000000000000000000000010000000000000000000000000000100000001000000000000000000000000000000000000000000000000000001010000000010000000000000000010001000000000000000000000"
"000000000000000000001000000000000000010000000000000000100011000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000010000000000100100000010000000"
"000000000000000000000000000000000000000000000000000000000000100000001000010000000000000000001000000010000000001000000000000000010000000000000000000000000000000000100000000000000000000000000000100100000000"
"000001100000000000000000000000000010000000000000000000000000010000000000000000000000000000000001000000100000000010000000000000001000000000000000000000000000000000010000000000000000000000000000000000010000"
"000000000000000100000000001000000000000000010000000000000000001000000000000000000000000100000000000000000000000000000000100000000000010000000000000000000000000000001000000000100000000000010000000000000000"
"000100000000000000000000010000000000000000000000000000000000000100000000000000100001000000000000000000000000000000000010000000000000010100000000000000000000000000000101000000000000000000000000000000000000"
"000000000001100000000000000000000000000000000000000100001000000010000000000000000000000000000000000000000100000000000000000000000000000001000100000000000000000000000010000000010000000000000000000000000000"
"000000000000000000000001000001000000000000000000000100000000000001000000000000000000000000000000000001000000000100001000010000000000001000000000000000000000000000000001000000000000000000000000000000000000"
"000001000000000000000000000000000000000000000000000000001000000000100000000000000000100000000000100000000000000000000000000000000000000000010000000000000000000000000000100000000000010101000000000000000000"
"000001000000000000000000000000000000000000000000000000000100000000010010000000000000000001000000000000000000000000000000000000000000010000000010100000010000000000000000010000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000000001001010000000001000000010000000000000000000000000110000000001000000000000000000000100000000000000000000000000000000001000000000000000000000000000000000"
"000000000000000000100001000000000000000000000000000000010000000000000100000000000000000000000000000100001000000001000000000000011000000000000000000000000000000000000000000100000000000000000000000000000000"
"000000000000010000000000000000000000000000000000000100000000001000000110000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000100010000100000000000000000100000000"
"000000000010000000000000100000000000001000000000000000000000000001000001000000000000000000000000000000000000000000000001000000000101000000000000000000000000000000000000000001000000000000000000000000100000"
"000000000000000000000000000000000000000000000001100000000000000100000000100000000000000000000000010000000000001000000000000000000000000000000000000000000000000000000000000000100010000000000000000000001100"
"000000001000001000000000000000000000000000000000000000000100000000000000010000000001000000000000000000000010000000000001000000000000001000000000000000000000000000010000000000010000000000000000000000000000"
"000000000000000000000000000100000000000010000000000000000000000000000100001000000000000000000001000000000000000000000000000000000000100000000000000000000010000000000100000000001000000000000000000000000100"
"000000000000000000000000010000000000000000001000000000000000000010000000000100000000000000000100000000000000000000000000000000000000000000000001000000000000010000000000000100000100000000000000000000000010"
"000000000100100000000000000000000000000000000000000000000000000000000000000010000001001000000000000000000000000000000000000000100000000000000000000000100000000000100000000000000010000000000010000000000000"
"000000000000000000000000000000000000000000000000100000100000000010000000000001000000000001000000000000000000000000000000010000000100000000100000000000000000000000000000000000000001010000000000000000000000"
"000000000000000000000000000000001000000000100000000000000000000000100000000000100000000000000000001000000000000000000000001000000000000000001000000000000000000000000000000000000000100000010000000000000100"
"000000000000000001100000000000000010000000000100000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000001000000000000010000000000000010000010000100000000000"
"000000001000000100000000000000000001000000000000000000100000000000000000000000001000000000000000000000000000000000000000000000000000000000000010000000000000000100000000000000000000001001000010000000000000"
"000000000110000000000000000000000000000000000000000000000000000000000000000000100100000000000001000000000001001000000000000001000000000000000000000000000000000000000000000100000000000100000000000000000000"
"000000000001000000000000000000000001000000000000000000000000000000001000000000000010001000000000000000000000000000000000000000000000000000000000000100000000000000000000000001000000000010000000000000000101"
"000000000000000000000000000000000000000000001011000000000000000000000000001000000001000000000000000000000001000000000000001000000000000000000100000000001000000000000000000000000000000001000000000000000000"
"000000001000000000000000000000000000000000000100000011000000000000000000000000000000100000000000000000000000000000100000100000001000000000000000000000000000000000000000000000001000000000100000000000000000"
"000000000000000000000100000000000000000000000000000000000000000100000000000000000010010000100000000000000010000000010000000000000000000000000000000000000000000000000000001000000000000000010000000000100000"
"000000010000000000000100000000000000000000000000000000000000100000000000000000001000001000000000000000010000000000000001000000000000000000000000000000000000000000000000000000000000010000001000000001000000"
"000000000000000000000000000000011000001000000000001000000000000000000000000000000000000100000000000000000000000000000000000000001000000000000010000000000000000000000000001000000001000000000100000000000000"
"001000000000000000001000000000000000000000000000000100000000000000000000000000000000000010000000000100100000100000100000000010000000000000000000000000000000000000000000000000000000000000000010000000000000"
"000000000000000000010000000000000000000100010000000000000000100000000000000000000000000001000000000000000000000000000100000000000000000010000000000000001000000000000000000000000000000100000001000000000000"
"000001000000000000000010000010000000000000000000000000000000000000000000000000000001000000100000000000000000000000000000000000000000000000000001000000000010000000000000000010000000000010000000100000000000"
"000000000000010000000000000000000000000000000000100000000000000000000001000000000000000000010000000001000000000000000000000000000000000000000000001000000000000000000000001000000000000011000000010000000000"
"000010000000000000001000000010000000000000000000000000000000000010000000000000000000000000001000000000001000010000000010000000000000000000000000000000000000000000000000000000000000000100000000001000000000"
"000000000000000000000000000000000000001000000000000000000000000000000000000000010100000000000100010000000010000010000000000000000010000000000000000000000000000000000100000000000000000000000000000100000000"
"000000000000000000000000000001000000000000000010010000000000001000000000000000000000000000000010000000010000000000000100000000000000000000000000000000000000000010100000000000000000000000000000000010000000"
"000000000000000000000000100000000100000000000000000000010000000000010000000000000000000000000001000000000000000000010000000000000000000000000000000000000000000000000000000000000000100001000000000101000000"
"000000000000000000000000000000000001010000000100000000000000000000000000000000000000000010000000100000000000000010000000000000000000000100000000000001000000100000000000000000000000000000000000000000100000"
"001000000001000001000000000001000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000100000000000000000000000000000000010000000000000000100000000001010000"
"000000000000000000000000000000000000000000000000000010000000010000000000010000000000000000000000101000010000000000000010000000000000000000000000000000000001000000000000000000000000000010000000000000001000"
"000000000000000100000000000000000000000000000000000000000000000000010000000000000100000000000010000100000100000000000000000100000000000000000000000000000000010000000000000000000000000000000000001000000100"
"000000000000000000100000000000000001000010000000000000000000000000000001000000000000000000000000000010000000000100000000000100000000000000000000000000000000000001010000000000000000000000000000000000000010"
"001000000000000000000000000000000000010000000000001000000000000000000000000000000000010000000000000001000000000000000000000000010000000000000000000000000000000000000000000010100000000100000000000000000001"
;

//#define ROWS 102
//#define COLS 204

int main() {
short int i,j,j2,i2;

short int temp;

const short int k = ROWS, r = ROWS, c = COLS, n = COLS, nmk = n - k;

/* construct H */
short int*pH = H[0];

for (i = 0; i < ROWS*COLS; ++i)
pH[i] = '1' == ache[i];

for(i=0;i<nmk;i++) {
j=nmk+i;
if(H[i][j] != 1) {
for(i2=i+1;i2<r;i2++) {
if(H[i2][j]) {		/* removed == 1 */
for(j2=0;j2<c;j2++) {
temp = H[i2][j2];
H[i2][j2] = H[i][j2];
H[i][j2] = temp;
}
break;
}
}
}
for(i2 = 0;i2<r;i2++)
if(H[i2][j] && i2 != i)
for(j2=0;j2<c;j2++)
H[i2][j2] ^=	H[i][j2]; /* substituted xor in place of some form of absolute value */
}

for(i=0;i<ROWS;i++, putchar('\n')) /* Inserted putchar('\n')) */
for(j=0;j<COLS;j++)
printf("%d",H[i][j]);

short int G[ROWS][COLS] = {0};
for (i=0;i<k;i++)		/* need only work along the diagonal, removing a nested loop */
G[i][i] = 1;

for(i=0;i<r;i++)		/* transpose some hunk */
for(j=0;j<k;j++)
G[j][k+i] = H[i][j];

for(i=0;i<ROWS;i++)
for(j=0;j<COLS;j++)
printf("%d",G[i][j]);

#if 0

short int s, C[ROWS];		/* used short ints for H, C, and G */
for(j=0;j<n;j++) {
for(s = i = 0;i<k;i++)
s ^= msg[i] & G[i][j];	/* replaced multiplication with bit operations */
C[j] = s;
}

for(i=0;i<n;i++)
printf("%d",C[i]);

#else

/* remove variable C, combine the two loops */

for(j=0;j<n;j++) {
for(temp = i = 0;i<k;i++)
temp ^= msg[i] & G[i][j];	/* replaced multiplication with bit operations */
printf("%d", temp);
}

#endif

return 0;
}```