## Exercise 3: General code optimization

Which optimization strategies would you suggest for the piece of code below? Write down what you expect to give the best performance. You may test your work by performing benchmark runs. You can find a working code in the folder OPT.

double mat[N][N], s[N][N], val;int i, j, v[N];//// ... v[] and s[][] may be assumed to contain valid data//for(i=0; i<N ; ++i) {  for(j=0; j<N; ++j) {    val = (double)(v[i] % 256);    mat[j][i] = s[j][i]*(sin(val)*sin(val)-cos(val)*cos(val));  }}

No assumptions about the size of N may be made. You may, however, assume that the code is part of a function that gets called very frequently. s[][] and v[] may change between calls, and all entries of v[] are positive. You are not allowed to just flip the indices on the matrices - this would change the result!

Further thoughts:

• How does the optimization strategy depend on the problem size?
• Can you predict an upper performance limit on one socket? Can you achieve this limit with your code?