Changeset 32

Show
Ignore:
Timestamp:
08/09/07 04:53:35 (5 years ago)
Author:
mike
Message:

Broke it

Location:
trunk
Files:
7 modified

Legend:

Unmodified
Added
Removed
  • trunk/benchmark.c

    r30 r32  
    2727 
    2828 
    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 } 
    6029 
    6130int main( int argc, char** argv) { 
  • trunk/block.c

    r30 r32  
    1515#include <stdlib.h> 
    1616#include <stdio.h> 
     17#include <string.h> 
    1718 
    1819#include "types.h" 
     
    137138} 
    138139 
     140void 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; 
     177life_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} 
     194void free_block( life_block *block ) { 
     195    if( !block ) { 
     196        return; 
     197    } 
     198    free( block->allocated ); 
     199    free( block ); 
     200} 
     201 
     202llife_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} 
     222void 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 
     234void 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} 
    139246//  XX d3 d2    a3 a2 a1    b3 b2 b1    c3 c2 c1 
    140247//  a3 a2 a1    b3 b2 b1    c3 c2 d1    d3 d2 d1  
  • trunk/block.h

    r27 r32  
    1414                    llife_block l, llife_block r, 
    1515                    llife_block bl, llife_block b, llife_block br ); 
     16 
     17static inline llife_block get_life_block( llife_world world, int row, int column ) { 
     18    return world->blocks[ (row*world->width + column) ]; 
     19} 
     20 
     21llife_world allocate_world( int height, int width ); 
     22void free_world( llife_world world ); 
     23 
     24void worldCycle( llife_world world ); 
     25 
     26//returns NULL if fail; 
     27life_block *allocate_block(); 
     28 
     29void free_block( life_block *block ); 
     30 
     31void set_bit_in_world( llife_world world, int row, int column ); 
    1632#endif //_BLOCK_H_INCLUDED 
  • trunk/glutmain.c

    r26 r32  
    2828#include "block.h" 
    2929 
    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 
     31llife_world my_world; 
    3232 
    3333void reshape( int width, int height ) { 
     
    4040    glLoadIdentity(); 
    4141    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 ); 
    4343    glMatrixMode(GL_MODELVIEW); 
    4444    glLoadIdentity(); 
     
    8787} 
    8888 
    89     llife_block origin[3][3]; 
    9089 
    9190void renderScene() { 
     
    9392    int i, j; 
    9493 
    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 ); 
    9898        } 
    9999    } 
     
    104104} 
    105105 
    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 } 
    135106void myTimer(int value) { 
    136107    glutTimerFunc(1000/60,myTimer,1); 
    137108 
    138     worldCycle(); 
     109    worldCycle( my_world ); 
    139110    glutPostRedisplay(); 
    140111} 
     
    142113int main( int argc, char** argv) { 
    143114    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     } 
    154115 
    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 ); 
    159116 
    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 ); 
    161120 
    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 ); 
    172138 
    173139    glutInit( &argc, argv ); 
     
    182148    glutReshapeFunc( reshape ); 
    183149 
     150 
    184151    glutTimerFunc(1000.*5,myTimer,1); 
    185152 
     
    187154    glutMainLoop(); 
    188155 
     156    printf( "meow\n"); 
    189157    return 0;    
    190158} 
  • trunk/helpers.c

    r27 r32  
    5353 
    5454 
    55 //returns NULL if fail; 
    56 life_block *allocate_block() { 
    57     //We're going to do two allocs so our data is alligned 
    58     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 easier 
    63      
    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 } 
    7755 
    7856void print_life_block( life_block *block ) { 
  • trunk/helpers.h

    r27 r32  
    6363 
    6464 
    65  
    66 //returns NULL if fail; 
    67 life_block *allocate_block(); 
    68  
    69 void free_block( life_block *block ); 
    70  
    7165void print_life_block( life_block *block ); 
    7266 
  • trunk/types.h

    r27 r32  
    1212#include "fakesse.h" 
    1313 
    14 typedef __m128i data_block[ 128 ]; 
     14#define BLOCK_HEIGHT 128 
     15typedef __m128i data_block[ BLOCK_HEIGHT ]; 
     16 
    1517typedef struct life_block { 
    1618    data_block *allocated; //this is where we free it 
    1719    data_block *current; 
    1820    data_block *working; 
     21//' struct life_block *n, *ne, *e, *se, *s, *sw, *w, *nw; 
     22} life_block, *llife_block; 
    1923 
    20     struct life_block *n, *ne, *e, *se, *s, *sw, *w, *nw; 
    21 } life_block, *llife_block; 
     24typedef 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 
    2245#endif //_TYPES_H_INCLUDED