- #1

- 62

- 0

## Homework Statement

I need to optimize this given code:

Code:

```
/* A struct used to compute averaged pixel value */
typedef struct {
int red;
int green;
int blue;
int num;
} pixel_sum;
/* Compute min and max of two integers, respectively */
static int min(int a, int b) { return (a < b ? a : b); }
static int max(int a, int b) { return (a > b ? a : b); }
/*
* initialize_pixel_sum - Initializes all fields of sum to 0
*/
static void initialize_pixel_sum(pixel_sum *sum)
{
sum->red = sum->green = sum->blue = 0;
sum->num = 0;
return;
}
/*
* accumulate_sum - Accumulates field values of p in corresponding
* fields of sum
*/
static void accumulate_sum(pixel_sum *sum, pixel p)
{
sum->red += (int) p.red;
sum->green += (int) p.green;
sum->blue += (int) p.blue;
sum->num++;
return;
}
/*
* assign_sum_to_pixel - Computes averaged pixel value in current_pixel
*/
static void assign_sum_to_pixel(pixel *current_pixel, pixel_sum sum)
{
current_pixel->red = (unsigned short) (sum.red/sum.num);
current_pixel->green = (unsigned short) (sum.green/sum.num);
current_pixel->blue = (unsigned short) (sum.blue/sum.num);
return;
}
/*
* avg - Returns averaged pixel value at (i,j)
*/
static pixel avg(int dim, int i, int j, pixel *src)
{
int ii, jj;
pixel_sum sum;
pixel current_pixel;
initialize_pixel_sum(&sum);
for(ii = max(i-1, 0); ii <= min(i+1, dim-1); ii++)
for(jj = max(j-1, 0); jj <= min(j+1, dim-1); jj++)
accumulate_sum(&sum, src[RIDX(ii, jj, dim)]);
assign_sum_to_pixel(¤t_pixel, sum);
return current_pixel;
}
/******************************************************
* Your different versions of the smooth kernel go here
******************************************************/
/*
* naive_smooth - The naive baseline version of smooth
*/
char naive_smooth_descr[] = "naive_smooth: Naive baseline implementation";
void naive_smooth(int dim, pixel *src, pixel *dst)
{
int i, j;
for (i = 0; i < dim; i++)
for (j = 0; j < dim; j++)
dst[RIDX(i, j, dim)] = avg(dim, i, j, src);
}
```

## Homework Equations

## The Attempt at a Solution

I figured the best approach would be to eliminate the function calls, but I'm getting a bunch of compiler errors that I can't figure out how to fix. I will post my code and then the compiler messages. Basically (for now, at least) I'm just looking for help on how to fix the errors I'm getting. From there I can move forward and tweak until I get at least a 3.5x speed up (required for a 100 on this assignment).

Code:

```
424: void smooth(int dim, pixel *src, pixel *dst)
425: {
426: int i, j;
427: pixel *current_pixel;
428: pixel_sum *sum;
429: pixel_sum *sum_red;
430: pixel_sum *sum_green;
431: pixel_sum *sum_blue;
432: pixel_sum *sum_num;
433: pixel p;
434: int red, green, blue, num;
435: int ii, jj;
436: int min_i, min_j, max_i, max_j;
437: int a_min_i, a_max_i, b_min, b_max;
438: int a_min_j, a_max_j;
439:
440:
441: for (i = 0; i < dim; i++){
442: for (j = 0; j < dim; j++){
443: sum = (int *)∑
444: sum_red = 0;
445: sum_green = 0;
446: sum_blue = 0;
447: sum_num = 0;
448:
449: a_max_i = i-1;
450: b_max = 0;
451: a_min_i = i+1;
452: b_min = dim-1;
453: max_i = (a_max_i > b_max ? a_max_i : b_max);
454: min_i = (a_min_i < b_min ? a_min_i : b_min);
455:
456: a_max_j = j-1;
457: a_min_j = j+1;
458: max_j = (a_max_j > b_max ? a_max_j : b_max);
459: min_i = (a_min_i < b_min ? a_min_j : b_min);
460: for(ii = max_i; ii <= min_i; ii++){
461: for(jj = max_j; jj <= min_j; jj++){
462: p = src[RIDX(ii, jj, dim)];
463: sum_red += (int) p.red;
464: sum_green += (int) p.green;
465: sum_blue += (int) p.blue;
466: sum_num++;
467:
468: }
469: }
470: current_pixel = (int *)¤t_pixel;
471: current_pixel->red = (unsigned short) (sum.red/sum.num);
472: current_pixel->green = (unsigned short) (sum.green/sum.num);
473: current_pixel->blue = (unsigned short) (sum.blue/sum.num);
474: dst[RIDX(i, j, dim)] = *current_pixel;
475: }
476: }
477:}
```

COMPILER:

Code:

```
kernels.c: In function âsmoothâ:
kernels.c:443: warning: dereferencing type-punned pointer will break strict-aliasing rules
kernels.c:443: warning: assignment from incompatible pointer type
kernels.c:470: warning: dereferencing type-punned pointer will break strict-aliasing rules
kernels.c:470: warning: assignment from incompatible pointer type
kernels.c:471: error: request for member âredâ in something not a structure or union
kernels.c:471: error: request for member ânumâ in something not a structure or union
kernels.c:472: error: request for member âgreenâ in something not a structure or union
kernels.c:472: error: request for member ânumâ in something not a structure or union
kernels.c:473: error: request for member âblueâ in something not a structure or union
kernels.c:473: error: request for member ânumâ in something not a structure or union
kernels.c:434: warning: unused variable ânumâ
kernels.c:434: warning: unused variable âblueâ
kernels.c:434: warning: unused variable âgreenâ
kernels.c:434: warning: unused variable âredâ
make: *** [kernels.o] Error 1
```

Any help would be greatly appreciated!