From 0ec6aae4af7ef0e79970007824f657b3f2547dbe Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 25 Aug 2014 16:23:44 +0000 Subject: [PATCH] host: split write_image for density/velocity, -lm split write_image() into write_density() and write_velocity(). velocity calculations needs square root, so include and link against libm. --- d2q9/Makefile | 2 +- d2q9/hsrc/data.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++-- d2q9/hsrc/main.c | 6 ++-- 3 files changed, 90 insertions(+), 6 deletions(-) diff --git a/d2q9/Makefile b/d2q9/Makefile index f4f22ed..28db1ce 100644 --- a/d2q9/Makefile +++ b/d2q9/Makefile @@ -3,7 +3,7 @@ # host toolchain HCC = gcc HCFLAGS = -O2 -std=c99 -I$(EPIPHANY_HOME)/tools/host/include -Wall -HLFLAGS = -L$(EPIPHANY_HOME)/tools/host/lib -le-hal +HLFLAGS = -lm -L$(EPIPHANY_HOME)/tools/host/lib -le-hal ECHO = /bin/echo -e # target toolchain diff --git a/d2q9/hsrc/data.c b/d2q9/hsrc/data.c index 487768d..8e709a1 100644 --- a/d2q9/hsrc/data.c +++ b/d2q9/hsrc/data.c @@ -2,6 +2,7 @@ #include #include +#include #include #include "../shared.h" @@ -54,15 +55,15 @@ void write_populations(d2q9_block_t lattice[CORES_Y][CORES_X], int iter) return; } -/* write an 8-bit grayscale, binary PPM image of the lattice */ -void write_image(d2q9_block_t lattice[CORES_Y][CORES_X], int iter) +/* write an 8-bit grayscale, binary PPM image of the particle density */ +void write_density(d2q9_block_t lattice[CORES_Y][CORES_X], int iter) { char name[32]; snprintf(name, 32, "./tmp/i%06d.ppm", iter); /* open image file and write header */ FILE *file = fopen(name, "wb"); if(!file) { - perror("write_image/fopen"); + perror("write_density/fopen"); return; } fprintf(file, "P5\n%d %d\n%d\n", @@ -107,6 +108,87 @@ void write_image(d2q9_block_t lattice[CORES_Y][CORES_X], int iter) return; } +/* write an 8-bit grayscale, binary PPM image of the particle velocity */ +void write_velocity(d2q9_block_t lattice[CORES_Y][CORES_X], int iter) +{ + char name[32]; snprintf(name, 32, "./tmp/i%06d.ppm", iter); + + /* open image file and write header */ + FILE *file = fopen(name, "wb"); + if(!file) { + perror("write_velocity/fopen"); + return; + } + fprintf(file, "P5\n%d %d\n%d\n", + CORES_X*BLOCKS_X, CORES_Y*BLOCKS_Y, 255); + + + /* calculate all velocities and remember min/max */ + FLOAT min = 1000, max = 0; + FLOAT us[CORES_Y][BLOCKS_Y][CORES_X][BLOCKS_X]; + for(int cy = 0; cy < CORES_Y; cy++) { + for(int y = 0; y < BLOCKS_Y; y++) { + for(int cx = 0; cx < CORES_X; cx++) { + for(int x = 0; x < BLOCKS_X; x++) { + FLOAT rho = ( + lattice[cy][cx][y][x][0] + + lattice[cy][cx][y][x][1] + + lattice[cy][cx][y][x][2] + + lattice[cy][cx][y][x][3] + + lattice[cy][cx][y][x][4] + + lattice[cy][cx][y][x][5] + + lattice[cy][cx][y][x][6] + + lattice[cy][cx][y][x][7] + + lattice[cy][cx][y][x][8] + ); + + FLOAT ux = ( + lattice[cy][cx][y][x][5] + + lattice[cy][cx][y][x][6] + + lattice[cy][cx][y][x][7] - + lattice[cy][cx][y][x][1] - + lattice[cy][cx][y][x][2] - + lattice[cy][cx][y][x][3] + ) / rho; + + FLOAT uy = ( + lattice[cy][cx][y][x][1] + + lattice[cy][cx][y][x][7] + + lattice[cy][cx][y][x][8] - + lattice[cy][cx][y][x][3] - + lattice[cy][cx][y][x][4] - + lattice[cy][cx][y][x][5] + ) / rho; + + FLOAT u = sqrtf(ux*ux + uy*uy); + if(u < min) min = u; + if(u > max) max = u; + us[cy][y][cx][x] = u; + } + } + } + } + + /* scale values and write them to the image */ + for(int cy = 0; cy < CORES_Y; cy++) { + for(int y = 0; y < BLOCKS_Y; y++) { + for(int cx = 0; cx < CORES_X; cx++) { + for(int x = 0; x < BLOCKS_X; x++) { + unsigned char gray; + gray = (255. * (us[cy][y][cx][x]-min) / (max-min)); + fwrite(&gray, 1, 1, file); + } + } + } + } + + /* close the file and chown if run with sudo */ + fclose(file); + fixsudo(name); + + return; +} + /* convert image files to animated gif ./tmp/anim.gif */ void write_animation(void) { diff --git a/d2q9/hsrc/main.c b/d2q9/hsrc/main.c index 48ec00a..712feb1 100644 --- a/d2q9/hsrc/main.c +++ b/d2q9/hsrc/main.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -15,8 +16,9 @@ /* helper functions */ void fixsudo(const char *filename); void write_populations(d2q9_block_t lattice[CORES_Y][CORES_X], int iter); -void write_image(d2q9_block_t lattice[CORES_Y][CORES_X], int iter); void write_animation(void); +void write_density (d2q9_block_t lattice[CORES_Y][CORES_X], int iter); +void write_velocity (d2q9_block_t lattice[CORES_Y][CORES_X], int iter); void write_timers(uint32_t timers[CORES_Y][CORES_X][TIMERS], uint32_t iter); /* globals */ @@ -92,7 +94,7 @@ int main() /* write data */ //write_populations(shm.lattice, shm.iteration); - write_image(shm.lattice, shm.iteration); + write_density(shm.lattice, shm.iteration); write_timers(shm.timers, shm.iteration); } /* ================================================================ */ -- 2.30.2