Create threadpool in c land
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good

This commit is contained in:
unknown 2024-02-29 22:22:50 -05:00
parent 1e16151860
commit 2dc54a8186
7 changed files with 80 additions and 2 deletions

View File

@ -56,12 +56,17 @@ INPUT_FILES="./src/chunkmask.c"
OUTPUT_FILE="./chunkmask.o" OUTPUT_FILE="./chunkmask.o"
gcc $COMPILE_FLAGS -I"$BASE_INCLUDE_DIR" -I"$OS_INCLUDE_DIR" $INPUT_FILES -o $OUTPUT_FILE gcc $COMPILE_FLAGS -I"$BASE_INCLUDE_DIR" -I"$OS_INCLUDE_DIR" $INPUT_FILES -o $OUTPUT_FILE
COMPILE_FLAGS="-c -fPIC -m64 -mavx -mavx2 -O1"
INPUT_FILES="./src/threadpool.c"
OUTPUT_FILE="./threadpool.o"
gcc $COMPILE_FLAGS -I"$BASE_INCLUDE_DIR" -I"$OS_INCLUDE_DIR" $INPUT_FILES -o $OUTPUT_FILE
#compile shared object file #compile shared object file
OUTPUT_FILE="libfluidsim$LIB_ENDING" OUTPUT_FILE="libfluidsim$LIB_ENDING"
COMPILE_FLAGS="-shared" COMPILE_FLAGS="-shared"
INPUT_FILES="densitystep.o velocitystep.o chunkmask.o" INPUT_FILES="densitystep.o velocitystep.o chunkmask.o threadpool.o"
gcc $COMPILE_FLAGS $INPUT_FILES -o $OUTPUT_FILE gcc $COMPILE_FLAGS $INPUT_FILES -o $OUTPUT_FILE
#move to resources #move to resources

View File

@ -121,6 +121,14 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_setBoundsToNeighbors
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_copyNeighbors JNIEXPORT void JNICALL Java_electrosphere_FluidSim_copyNeighbors
(JNIEnv *, jobject, jint, jint, jint, jint, jobjectArray); (JNIEnv *, jobject, jint, jint, jint, jint, jobjectArray);
/*
* Class: electrosphere_FluidSim
* Method: createThreadpool
* Signature: (I)J
*/
JNIEXPORT jlong JNICALL Java_electrosphere_FluidSim_createThreadpool
(JNIEnv *, jclass, jint);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -0,0 +1,11 @@
#ifndef THREADPOOL
#define THREADPOOL
typedef struct {
int numThreads;
pthread_t * threads;
} ThreadPool;
#endif

View File

@ -2,7 +2,6 @@
#include <stdio.h> #include <stdio.h>
#include <immintrin.h> #include <immintrin.h>
#include <stdint.h> #include <stdint.h>
#include <pthread.h>
#include "../includes/libfluidsim.h" #include "../includes/libfluidsim.h"
#include "../includes/utilities.h" #include "../includes/utilities.h"

View File

@ -0,0 +1,45 @@
#include <jni.h>
#include <stdio.h>
#include <immintrin.h>
#include <stdint.h>
#include <unistd.h>
#include <pthread.h>
#include "../includes/libfluidsim.h"
#include "../includes/threadpool.h"
/**
* Main loops for the thread
*/
void * main_thread_loop(void * data);
/*
* Class: electrosphere_FluidSim
* Method: createThreadpool
* Signature: (I)J
*/
JNIEXPORT jlong JNICALL Java_electrosphere_FluidSim_createThreadpool
(JNIEnv * env,
jclass class,
jint numThreads){
ThreadPool * pool = (ThreadPool *)malloc(sizeof(ThreadPool));
pool->threads = (pthread_t *)malloc(sizeof(pthread_t) * numThreads);
pool->numThreads = numThreads;
int retCode = 0;
for(int i = 0; i < pool->numThreads; i++){
retCode = pthread_create(&pool->threads[i], NULL, main_thread_loop, NULL);
// printf("thread ret code %d\n",retCode);
}
for(int i = 0; i < pool->numThreads; i++){
pthread_join(pool->threads[i], NULL);
printf("resolve thread\n");
}
}
void * main_thread_loop(void * data){
printf("thread work\n");
sleep(1);
}

View File

@ -70,6 +70,8 @@ public class FluidSim {
float[] w0ArrayView = new float[DIM * DIM * DIM]; float[] w0ArrayView = new float[DIM * DIM * DIM];
int chunkMask = 0; int chunkMask = 0;
public static long threadpool;
static final float DIFFUSION_CONSTANT = 0.0f; static final float DIFFUSION_CONSTANT = 0.0f;
@ -849,6 +851,12 @@ public class FluidSim {
private native void copyNeighbors(int DIM_X, int chunkMask, int x, int vectorDir, ByteBuffer[] neighborMap); private native void copyNeighbors(int DIM_X, int chunkMask, int x, int vectorDir, ByteBuffer[] neighborMap);
public static long createThreadpoolWrapper(int numThreads){
return createThreadpool(numThreads);
}
private static native long createThreadpool(int numThreads);

View File

@ -83,6 +83,8 @@ public class Main {
} }
private static FluidSim[][][] initFluidSim(int dimx, int dimy, int dimz){ private static FluidSim[][][] initFluidSim(int dimx, int dimy, int dimz){
FluidSim.threadpool = FluidSim.createThreadpoolWrapper(5);
FluidSim[][][] simArray = new FluidSim[dimx][dimy][dimz]; FluidSim[][][] simArray = new FluidSim[dimx][dimy][dimz];
for(int x = 0; x < simArray.length; x++){ for(int x = 0; x < simArray.length; x++){
for(int y = 0; y < simArray[0].length; y++){ for(int y = 0; y < simArray[0].length; y++){