Changeset 32
Legend:
- Unmodified
- Added
- Removed
-
trunk/benchmark.c
r30 r32 27 27 28 28 29 void worldCycle() {30 int i, j;31 //#pragma omp parallel private(i)32 //#pragma omp for schedule(static)33 for( i = 0; i < WIDTH; i++ ) {34 for( j = 0; j < HEIGHT; j++ ) {35 llife_block tl, t, tr, l, r, bl, b, br;36 tl = i > 0 && j > 0 ? origin[i-1][j-1] : NULL;37 tr = i < WIDTH-1 && j > 0 ? origin[i+1][j-1] : NULL;38 t = j > 0 ? origin[i][j-1] : NULL;39 40 l = i > 0 ? origin[i-1][j] : NULL;41 r = i < WIDTH-1 ? origin[i+1][j] : NULL;42 43 bl = i > 0 && j < HEIGHT-1 ? origin[i-1][j+1] : NULL;44 br = i < WIDTH-1 && j < HEIGHT-1 ? origin[i+1][j+1] : NULL;45 b = j < HEIGHT-1 ? origin[i][j+1] : NULL;46 47 48 cycle_block( origin[i][j], tl, t, tr, l, r, bl, b, br );49 }50 }51 52 for( i = 0; i < WIDTH; i++ ) {53 for( j = 0; j < HEIGHT; j++ ) {54 data_block * mtemp = origin[i][j]->current;55 origin[i][j]->current = origin[i][j]->working;56 origin[i][j]->working = mtemp;57 }58 }59 }60 29 61 30 int main( int argc, char** argv) { -
trunk/block.c
r30 r32 15 15 #include <stdlib.h> 16 16 #include <stdio.h> 17 #include <string.h> 17 18 18 19 #include "types.h" … … 137 138 } 138 139 140 void worldCycle( llife_world world ) { 141 int i, j; 142 //#pragma omp parallel private(i) 143 //#pragma omp for schedule(static) 144 for( i = 0; i < world->width; i++ ) { 145 for( j = 0; j < world->height; j++ ) { 146 llife_block center = get_life_block( world, j, i); 147 if( center ) { 148 llife_block tl, t, tr, l, r, bl, b, br; 149 150 tl = i > 0 && j > 0 ? get_life_block( world, j-1, i-1 ) : NULL; 151 tr = i < world->width-1 && j > 0 ? get_life_block( world, j-1, i+1 ) : NULL; 152 t = j > 0 ? get_life_block( world, j-1, i ) : NULL; 153 154 l = i > 0 ? get_life_block( world, j, i-1 ) : NULL; 155 r = i < world->width-1 ? get_life_block( world, j, i+1 ) : NULL; 156 157 bl = i > 0 && j < world->height-1 ? get_life_block( world, j+1, i-1 ) : NULL; 158 br = i < world->width-1 && j < world->height-1 ? get_life_block( world, j+1, i+1 ) : NULL; 159 b = j < world->height-1 ? get_life_block( world, j+1, i ) : NULL; 160 161 162 cycle_block( center, tl, t, tr, l, r, bl, b, br ); 163 } 164 } 165 } 166 167 for( i = 0; i < world->width * world->height; i++ ) { 168 169 llife_block center = world->blocks[i]; 170 data_block * mtemp = center->current; 171 center->current = center->working; 172 center->working = mtemp; 173 } 174 } 175 176 //returns NULL if fail; 177 life_block *allocate_block() { 178 //We're going to do two allocs so our data is alligned 179 llife_block lb = calloc( 1, sizeof( life_block ) ); 180 if( !lb ) { 181 return 0; 182 } 183 //Allocate both of the buffers in 1 chunk. It will make life easier 184 185 lb->allocated = (data_block*)calloc( 1, sizeof( life_block ) * 2 + 0x7F );// calloc( 2, sizeof( __m128i ) * 128 ); 186 if( !lb->allocated ) { 187 free( lb ); 188 return 0; 189 } 190 lb->current = (data_block*)((((unsigned long)lb->allocated) + 0x7F) & ~0x7F); 191 lb->working = &(lb->current)[1]; 192 return lb; 193 } 194 void free_block( life_block *block ) { 195 if( !block ) { 196 return; 197 } 198 free( block->allocated ); 199 free( block ); 200 } 201 202 llife_world allocate_world( int height, int width ) { 203 204 llife_world world = malloc( sizeof( life_world ) ); 205 world->blocks = calloc( width * height, sizeof( llife_block ) ); 206 207 int i; 208 for( i = 0; i < width * height; i++ ) { 209 llife_block blk = allocate_block(); 210 if( !blk ) { 211 free_world( world ); 212 return 0; 213 } 214 world->blocks[i] = blk; 215 } 216 217 world->width = width; 218 world->height = height; 219 world->origin_x = world->origin_y = 0; 220 return world; 221 } 222 void free_world( llife_world world ) { 223 224 if( !world ) { 225 return; 226 } 227 int i; 228 for( i = 0; i < world->height * world->width; i++ ) { 229 free_block( world->blocks[i] ); 230 } 231 free( world->blocks ); 232 } 233 234 void set_bit_in_world( llife_world world, int row, int column ) { 235 int block_column = column / 128; 236 int block_row = row / BLOCK_HEIGHT; 237 238 int inner_column = column % 128; 239 int inner_row = row % BLOCK_HEIGHT; 240 241 llife_block blk = get_life_block( world, block_row, block_column ); 242 243 244 set_bit_in_block( blk, inner_row, inner_column ); 245 } 139 246 // XX d3 d2 a3 a2 a1 b3 b2 b1 c3 c2 c1 140 247 // a3 a2 a1 b3 b2 b1 c3 c2 d1 d3 d2 d1 -
trunk/block.h
r27 r32 14 14 llife_block l, llife_block r, 15 15 llife_block bl, llife_block b, llife_block br ); 16 17 static inline llife_block get_life_block( llife_world world, int row, int column ) { 18 return world->blocks[ (row*world->width + column) ]; 19 } 20 21 llife_world allocate_world( int height, int width ); 22 void free_world( llife_world world ); 23 24 void worldCycle( llife_world world ); 25 26 //returns NULL if fail; 27 life_block *allocate_block(); 28 29 void free_block( life_block *block ); 30 31 void set_bit_in_world( llife_world world, int row, int column ); 16 32 #endif //_BLOCK_H_INCLUDED -
trunk/glutmain.c
r26 r32 28 28 #include "block.h" 29 29 30 int top = 128*0-1, bottom = 128*3+1, left = 128*0-1, right = 128*3+1; 31 float width = 128*3+1, height = 128*3+1;30 31 llife_world my_world; 32 32 33 33 void reshape( int width, int height ) { … … 40 40 glLoadIdentity(); 41 41 glViewport(0, 0, width, height); 42 glOrtho( left, right, bottom, top, -1.0, 1.0 );42 glOrtho( 0, my_world->width * 128, my_world->height * BLOCK_HEIGHT, 0, -1.0, 1.0 ); 43 43 glMatrixMode(GL_MODELVIEW); 44 44 glLoadIdentity(); … … 87 87 } 88 88 89 llife_block origin[3][3];90 89 91 90 void renderScene() { … … 93 92 int i, j; 94 93 95 for( i = 0; i < 3; i++ ) { 96 for( j = 0; j < 3; j++ ) { 97 renderBlock( origin[i][j], i*128, j*128 ); 94 for( i = 0; i < my_world->width; i++ ) { 95 for( j = 0; j < my_world->height; j++ ) { 96 printf( "%x\n", *my_world->blocks ); 97 renderBlock( get_life_block( my_world, j, i ) , i*128, j*BLOCK_HEIGHT ); 98 98 } 99 99 } … … 104 104 } 105 105 106 void worldCycle() {107 int i, j;108 for( i = 0; i < 3; i++ ) {109 for( j = 0; j < 3; j++ ) {110 llife_block tl, t, tr, l, r, bl, b, br;111 tl = i > 0 && j > 0 ? origin[i-1][j-1] : NULL;112 tr = i < 2 && j > 0 ? origin[i+1][j-1] : NULL;113 t = j > 0 ? origin[i][j-1] : NULL;114 115 l = i > 0 ? origin[i-1][j] : NULL;116 r = i < 2 ? origin[i+1][j] : NULL;117 118 bl = i > 0 && j < 2 ? origin[i-1][j+1] : NULL;119 br = i < 2 && j < 2 ? origin[i+1][j+1] : NULL;120 b = j < 2 ? origin[i][j+1] : NULL;121 122 123 cycle_block( origin[i][j], tl, t, tr, l, r, bl, b, br );124 }125 }126 127 for( i = 0; i < 3; i++ ) {128 for( j = 0; j < 3; j++ ) {129 data_block * mtemp = origin[i][j]->current;130 origin[i][j]->current = origin[i][j]->working;131 origin[i][j]->working = mtemp;132 }133 }134 }135 106 void myTimer(int value) { 136 107 glutTimerFunc(1000/60,myTimer,1); 137 108 138 worldCycle( );109 worldCycle( my_world ); 139 110 glutPostRedisplay(); 140 111 } … … 142 113 int main( int argc, char** argv) { 143 114 int i, j; 144 for( i = 0; i < 3; i++ ) {145 for( j = 0; j < 3; j++ ) {146 origin[i][j] = allocate_block();147 if( !origin[i][j] ) {148 fprintf( stderr, "Allocation failed\n" );149 fflush( stderr );150 exit( 1 );151 }152 }153 }154 115 155 set_bit_in_block( origin[2][2], 50, 50 );156 set_bit_in_block( origin[2][2], 51, 50 );157 set_bit_in_block( origin[2][2], 52, 50 );158 set_bit_in_block( origin[2][2], 54, 50 );159 116 160 set_bit_in_block( origin[2][2], 50, 51 ); 117 my_world = allocate_world( 1, 1 ); 118 *(my_world->blocks) = allocate_block(); 119 printf( "%x\n", *my_world->blocks ); 161 120 162 set_bit_in_block( origin[2][2], 53, 52 ); 163 set_bit_in_block( origin[2][2], 54, 52 ); 164 165 set_bit_in_block( origin[2][2], 51, 53 ); 166 set_bit_in_block( origin[2][2], 52, 53 ); 167 set_bit_in_block( origin[2][2], 54, 53 ); 168 169 set_bit_in_block( origin[2][2], 50, 54 ); 170 set_bit_in_block( origin[2][2], 52, 54 ); 171 set_bit_in_block( origin[2][2], 54, 54 ); 121 // set_bit_in_world( my_world, 50, 50 ); 122 // set_bit_in_world( my_world, 51, 50 ); 123 // set_bit_in_world( my_world, 52, 50 ); 124 // set_bit_in_world( my_world, 54, 50 ); 125 // 126 // set_bit_in_world( my_world, 50, 51 ); 127 // 128 // set_bit_in_world( my_world, 53, 52 ); 129 // set_bit_in_world( my_world, 54, 52 ); 130 // 131 // set_bit_in_world( my_world, 51, 53 ); 132 // set_bit_in_world( my_world, 52, 53 ); 133 // set_bit_in_world( my_world, 54, 53 ); 134 // 135 // set_bit_in_world( my_world, 50, 54 ); 136 // set_bit_in_world( my_world, 52, 54 ); 137 // set_bit_in_world( my_world, 54, 54 ); 172 138 173 139 glutInit( &argc, argv ); … … 182 148 glutReshapeFunc( reshape ); 183 149 150 184 151 glutTimerFunc(1000.*5,myTimer,1); 185 152 … … 187 154 glutMainLoop(); 188 155 156 printf( "meow\n"); 189 157 return 0; 190 158 } -
trunk/helpers.c
r27 r32 53 53 54 54 55 //returns NULL if fail;56 life_block *allocate_block() {57 //We're going to do two allocs so our data is alligned58 life_block *lb = calloc( 1, sizeof( life_block ) );59 if( !lb ) {60 return 0;61 }62 //Allocate both of the buffers in 1 chunk. It will make life easier63 64 lb->allocated = (data_block*)calloc( 1, sizeof( data_block ) * 2 + 0x7F );// calloc( 2, sizeof( __m128i ) * 128 );65 lb->current = (data_block*)((((unsigned long)lb->allocated) + 0x7F) & ~0x7F);66 if( !lb->current ) {67 free( lb );68 return 0;69 }70 lb->working = &((lb->current)[1]);71 return lb;72 }73 void free_block( life_block *block ) {74 free( block->allocated );75 free( block );76 }77 55 78 56 void print_life_block( life_block *block ) { -
trunk/helpers.h
r27 r32 63 63 64 64 65 66 //returns NULL if fail;67 life_block *allocate_block();68 69 void free_block( life_block *block );70 71 65 void print_life_block( life_block *block ); 72 66 -
trunk/types.h
r27 r32 12 12 #include "fakesse.h" 13 13 14 typedef __m128i data_block[ 128 ]; 14 #define BLOCK_HEIGHT 128 15 typedef __m128i data_block[ BLOCK_HEIGHT ]; 16 15 17 typedef struct life_block { 16 18 data_block *allocated; //this is where we free it 17 19 data_block *current; 18 20 data_block *working; 21 //' struct life_block *n, *ne, *e, *se, *s, *sw, *w, *nw; 22 } life_block, *llife_block; 19 23 20 struct life_block *n, *ne, *e, *se, *s, *sw, *w, *nw; 21 } life_block, *llife_block; 24 typedef struct life_world { 25 //Not all blocks are allocated, make sure you check 26 llife_block *blocks; 27 28 int width; 29 int height; 30 31 int origin_x; 32 int origin_y; 33 34 } life_world, *llife_world; 35 36 #define NEEDS_TOP 0x01 37 #define NEEDS_TOP_RIGHT 0x02 38 #define NEEDS_TOP_LEFT 0x04 39 #define NEEDS_LEFT 0x08 40 #define NEEDS_RIGHT 0x10 41 #define NEEDS_BOTTOM 0x20 42 #define NEEDS_BOTTOM_LEFT 0x40 43 #define NEEDS_BOTTOM_RIGHT 0x80 44 22 45 #endif //_TYPES_H_INCLUDED
