Created
August 6, 2016 12:03
-
-
Save kell18/a8610b829f8f5e20e2cd925b4e41673a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Правила игры "Жизнь" | |
если потенциал клетки равен двум, то клетка сохраняет свое состояние; | |
если потенциал равен трем, то клетка оживает; | |
если потенциал меньше двух или больше трех, то клетка погибает. | |
*/ | |
#include <iostream> | |
#include <time.h> | |
#include <stdlib.h> /* srand, rand */ | |
#include <omp.h> | |
#include <immintrin.h> | |
#include <iostream> | |
#include <iomanip> | |
typedef __int64 COUNTER_TYPE; | |
typedef double(*LIFE_FUNC) (int seed, size_t dimension, size_t n_iterations, size_t& alive); | |
#define RUN_TEST(NAME,SEED,DIM,NIT,EXP) testRunner(NAME,SEED,DIM,NIT,EXP,#NAME); | |
double lifeOriginal(int seed, size_t dimension, size_t n_iterations, size_t& alive); | |
double lifePOptimized(int seed, size_t dimension, size_t n_iterations, size_t& alive); | |
void printall(__int8* table, COUNTER_TYPE width, COUNTER_TYPE height) | |
{ | |
for (COUNTER_TYPE i = 0; i < height; i++) | |
{ | |
for (COUNTER_TYPE j = 0; j < width; j++) | |
{ | |
std::cout << std::setw(1) << table[width * i + j] << " "; | |
} | |
std::cout << std::endl; | |
} | |
} | |
void testRunner(LIFE_FUNC func, int seed, size_t dimension, size_t n_iterations, size_t expected, const char* tag = NULL) | |
{ | |
double t = omp_get_wtime(); | |
int n_reps = 2; | |
double avg_time = 0; | |
char* printTag = tag ? tag : ""; | |
size_t alive; | |
func(seed, dimension, n_iterations, alive); | |
for (int i = 0; i < n_reps; i++) | |
{ | |
avg_time += func(seed, dimension, n_iterations, alive); | |
if (alive != expected) | |
{ | |
std::cout << printTag << " NOT PASSED: " << expected << "!=" << alive << std::endl; | |
return; | |
} | |
} | |
avg_time /= n_reps; | |
if (alive == expected) | |
{ | |
std::cout << printTag << " PASSED: " << avg_time << std::endl; | |
} | |
std::cout << "Run time = " << omp_get_wtime() - t << std::endl; | |
} | |
#define RunAllTests(func) RUN_TEST(func, 0, 1001, 2, 250236); RUN_TEST(func, 0, 5021, 2, 6151871); //0.1 | |
int main(int argc, char** argv) | |
{ | |
RunAllTests(lifeOriginal); | |
RunAllTests(lifePOptimized); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment