diff options
author | Bernhard Rosenkranzer <bernhard.rosenkranzer@linaro.org> | 2012-04-11 15:20:57 +0530 |
---|---|---|
committer | Amit Pundir <amit.pundir@linaro.org> | 2012-04-11 15:22:23 +0530 |
commit | d36241d407706b450790e02e3518a371f9b1303e (patch) | |
tree | 3c19bb90202bd4b1b760a4526d65f376d63b4515 | |
download | smp_test-linaro_android.tar.gz |
smp test: memset benchmarkHEADmasterlinaro_android
If all goes well, it prints some benchmark results (libc memset vs.
simple memset vs. parallelized simple memset vs. double-parallelized
simple memset) and exits with exit status 0.
If it doesn't go well, it tells what went wrong on stderr and exits
with a non-zero exit status.
Note that you'll also get a non-zero exit status if you run it on a
single-core box when all goes well -- since it's an SMP test, it
considers running things in a single thread an error (e.g. multiple
cores not being detected, e.g. because CONFIG_SMP is disabled in the
kernel).
-rw-r--r-- | smp.c | 127 |
1 files changed, 127 insertions, 0 deletions
@@ -0,0 +1,127 @@ +#include <omp.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/time.h> + +void *simple_memset(void *s, int c, size_t n) { + char *m=(char*)s; + for(int i=0; i<n; i++) + m[i]=c; + return s; +} + +void *parallel_memset(void *s, int c, size_t n) { + char *m=(char*)s; + #pragma omp parallel for + for(int i=0; i<n; i++) + m[i]=c; + return s; +} + +int main(int argc, char **argv) { + static int threads=0; + static int ret=0; + #pragma omp parallel shared(threads) num_threads(3) + { + int thread = omp_get_thread_num(); + #pragma omp barrier + if(thread == 0) + threads = omp_get_num_threads(); + } + printf("Number of threads (3 requested): %u\n", threads); + if(threads != 3) { + fprintf(stderr, "Wrong number of threads (3 requested manually): %u\n", threads); + ret++; + } + + #pragma omp parallel shared(threads) + { + int thread = omp_get_thread_num(); + #pragma omp barrier + if(thread == 0) + threads = omp_get_num_threads(); + } + printf("OpenMP wants to use %u threads\n", threads); + if(threads<2) { + fputs("OpenMP doesn't want to multi-thread.\nIf you aren't on a single-core machine, this is an SMP error.\n", stderr); + ret++; + } + + char pattern[100000]; + memset(pattern, 170 /* 10101010 */, 100000); + + char *s=(char*)malloc(100000); + memset(s, 0, 100000); + struct timeval start, end; + double start_, end_; + gettimeofday(&start, 0); + for(int i=0; i<100000; i++) { + memset(s, 170 /* 10101010 */, 100000); + if(memcmp(s, pattern, 100000)) { + fputs("Incorrect result from memset", stderr); + ret++; + break; + } + } + gettimeofday(&end, 0); + start_ = start.tv_sec + (double)start.tv_usec/1000000.0; + end_ = end.tv_sec + (double)end.tv_usec/1000000.0; + printf("libc memset: %f\n", end_-start_); + + memset(s, 0, 100000); + gettimeofday(&start, 0); + for(int i=0; i<100000; i++) { + simple_memset(s, 170, 100000); + if(memcmp(s, pattern, 100000)) { + fputs("Incorrect result from simple_memset", stderr); + ret++; + break; + } + } + gettimeofday(&end, 0); + start_ = start.tv_sec + (double)start.tv_usec/1000000.0; + end_ = end.tv_sec + (double)end.tv_usec/1000000.0; + double simple = end_-start_; + printf("simple memset: %f\n", simple); + + memset(s, 0, 100000); + gettimeofday(&start, 0); + for(int i=0; i<100000; i++) { + parallel_memset(s, 170, 100000); + if(memcmp(s, pattern, 100000)) { + fputs("Incorrect result from parallel_memset", stderr); + ret++; + break; + } + } + gettimeofday(&end, 0); + start_ = start.tv_sec + (double)start.tv_usec/1000000.0; + end_ = end.tv_sec + (double)end.tv_usec/1000000.0; + double parallel = end_-start_; + printf("parallel memset: %f\n", parallel); + + memset(s, 0, 100000); + gettimeofday(&start, 0); + #pragma omp parallel for + for(int i=0; i<100000; i++) { + parallel_memset(s, 170, 100000); + if(memcmp(s, pattern, 100000)) { + fputs("Incorrect result from parallel_memset", stderr); + ret++; + } + } + gettimeofday(&end, 0); + start_ = start.tv_sec + (double)start.tv_usec/1000000.0; + end_ = end.tv_sec + (double)end.tv_usec/1000000.0; + printf("double-parallel memset: %f\n", parallel); + + free(s); + + if(parallel>simple && threads >= 2) { + fputs("Parallel memset slower than simple memset even though multiple\nthreads are enabled. This is an indication of a possible SMP error.\n", stderr); + ret++; + } + + return ret; +} |