diff --git a/src/threadpool.h b/src/threadpool.h index 02eacf9317cf197b610fef09f8337fab4a106cd6..729543fd59bc5cf0a8110bd2b54ef78a0d7c7de1 100644 --- a/src/threadpool.h +++ b/src/threadpool.h @@ -43,11 +43,11 @@ struct threadpool { /* Current map data and count. */ void *map_data, *map_extra_data; - size_t map_data_count, map_data_size, map_data_stride; - threadpool_map_function map_function; + volatile size_t map_data_count, map_data_size, map_data_stride; + volatile threadpool_map_function map_function; /* Counter for the number of threads that are done. */ - int num_threads_done; + volatile int num_threads_done; }; /* Function prototypes. */ diff --git a/tests/Makefile.am b/tests/Makefile.am index d0c132ad1b6dadd749a389fb71b873120b48139a..94cb85310cd3e2a693537e5d180184205c2ee1fd 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -22,11 +22,12 @@ AM_LDFLAGS = ../src/.libs/libswiftsim.a $(HDF5_LDFLAGS) $(HDF5_LIBS) # List of programs and scripts to run in the test suite TESTS = testGreetings testReading.sh testSingle testPair.sh testPairPerturbed.sh \ - test27cells.sh test27cellsPerturbed.sh testParser.sh testKernel + test27cells.sh test27cellsPerturbed.sh testParser.sh testKernel \ + threadpool_test # List of test programs to compile check_PROGRAMS = testGreetings testReading testSingle testTimeIntegration \ - testSPHStep testPair test27cells testParser testKernel + testSPHStep testPair test27cells testParser testKernel threadpool_test # Sources for the individual programs testGreetings_SOURCES = testGreetings.c @@ -47,6 +48,8 @@ testParser_SOURCES = testParser.c testKernel_SOURCES = testKernel.c +threadpool_test_SOURCES = threadpool_test.c + # Files necessary for distribution EXTRA_DIST = testReading.sh makeInput.py testPair.sh testPairPerturbed.sh \ test27cells.sh test27cellsPerturbed.sh tolerance.dat testParser.sh \ diff --git a/tests/testKernel.c b/tests/testKernel.c index b19eb8f69f1390c6da6f92bc9b8eb2b968a11266..9ba50ee049201cede5166528fa67153838a707da 100644 --- a/tests/testKernel.c +++ b/tests/testKernel.c @@ -18,7 +18,7 @@ * ******************************************************************************/ -#define NO__AVX__ +#define NO__SSE2__ #include "vector.h" #include "swift.h" diff --git a/tests/threadpool_test.c b/tests/threadpool_test.c new file mode 100644 index 0000000000000000000000000000000000000000..9a7d6b1db4962e431ba15f324eb22e5792f98dfa --- /dev/null +++ b/tests/threadpool_test.c @@ -0,0 +1,70 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (C) 2016 Pedro Gonnet (pedro.gonnet@durham.ac.uk) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ + +// Standard includes. +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +// Local includes. +#include "../src/threadpool.h" +#include "../src/atomic.h" + +void map_function_first(void *map_data, void *extra_data) { + const int input = *(int *)map_data; + usleep(rand() % 1000000); + printf("map_function_first: got input %i.\n", input); + fflush(stdout); +} + +void map_function_second(void *map_data, void *extra_data) { + const int input = *(int *)map_data; + usleep(rand() % 1000000); + printf("map_function_second: got input %i.\n", input); + fflush(stdout); +} + +int main(int argc, char *argv[]) { + + // Some constants for this test. + const int num_threads = 16; + const int N = 20; + const int num_runs = 2; + + // Create a threadpool with 8 threads. + struct threadpool tp; + threadpool_init(&tp, num_threads); + + // Main loop. + for (int run = 0; run < num_runs; run++) { + + // Run over a set of integers and print them. + int data[N]; + for (int k = 0; k < N; k++) data[k] = k; + printf("processing integers from 0..%i.\n", N); + fflush(stdout); + threadpool_map(&tp, map_function_first, data, N, sizeof(int), NULL); + + // Do the same thing again, with less jobs than threads. + printf("processing integers from 0..%i.\n", num_threads / 2); + fflush(stdout); + threadpool_map(&tp, map_function_second, data, num_threads / 2, sizeof(int), + NULL); + } +}