work to setup migration to c

This commit is contained in:
unknown 2024-03-04 21:23:36 -05:00
parent e605a82df6
commit 12dfd9e6b4
4 changed files with 176 additions and 57 deletions

View File

@ -56,12 +56,17 @@ INPUT_FILES="./chunkmask.c"
OUTPUT_FILE="./chunkmask.o"
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="./fluidsim.c"
OUTPUT_FILE="./fluidsim.o"
gcc $COMPILE_FLAGS -I"$BASE_INCLUDE_DIR" -I"$OS_INCLUDE_DIR" $INPUT_FILES -o $OUTPUT_FILE
#compile shared object file
OUTPUT_FILE="libfluidsim$LIB_ENDING"
COMPILE_FLAGS="-shared"
INPUT_FILES="densitystep.o velocitystep.o chunkmask.o"
INPUT_FILES="densitystep.o velocitystep.o chunkmask.o fluidsim.o"
gcc $COMPILE_FLAGS $INPUT_FILES -o $OUTPUT_FILE
#move to resources

45
src/main/c/fluidsim.c Normal file
View File

@ -0,0 +1,45 @@
#include <jni.h>
#include <stdint.h>
#include "includes/utilities.h"
#include "includes/chunkmask.h"
#include "includes/electrosphere_FluidSim.h"
#define DIM 18
#define LINEARSOLVERTIMES 20
typedef struct {
float * d;
float * u;
float * v;
float * w;
float * d0;
float * u0;
float * v0;
float * w0;
} Chunk;
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_simulate(
JNIEnv * env,
jclass class,
jfloat timestep
){
}
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_queueChunk(
JNIEnv * env,
jclass class,
jint oldDim,
jint chunkmask,
jobjectArray dr,
jobjectArray ur,
jobjectArray vr,
jobjectArray wr,
jobjectArray d0r,
jobjectArray u0r,
jobjectArray v0r,
jobjectArray w0r,
jfloat DIFFUSION_CONSTANT,
jfloat VISCOSITY_CONSTANT
){
}

View File

@ -17,14 +17,6 @@ extern "C" {
#define electrosphere_FluidSim_LINEARSOLVERTIMES 20L
#undef electrosphere_FluidSim_GRAVITY
#define electrosphere_FluidSim_GRAVITY -100.0f
/*
* Class: electrosphere_FluidSim
* Method: simulate
* Signature: (II[Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
*/
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_simulate
(JNIEnv *, jobject, jint, jint, jobjectArray, jobject, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jfloat, jfloat, jfloat);
/*
* Class: electrosphere_FluidSim
* Method: calculateChunkMask
@ -121,6 +113,22 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_setBoundsToNeighbors
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_copyNeighbors
(JNIEnv *, jobject, jint, jint, jint, jint, jobjectArray);
/*
* Class: electrosphere_FluidSim
* Method: queueChunk
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FF)V
*/
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_queueChunk
(JNIEnv *, jclass, jint, jint, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jfloat, jfloat);
/*
* Class: electrosphere_FluidSim
* Method: simulate
* Signature: (F)V
*/
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_simulate
(JNIEnv *, jclass, jfloat);
#ifdef __cplusplus
}
#endif

View File

@ -172,24 +172,34 @@ public class FluidSim {
}
}
//
//Vector stage
solveChunkMask(simArray);
addVectorSources(simArray, timestep);
swapAllVectorFields(simArray, timestep);
solveVectorDiffusion(simArray, timestep);
solveProjection(simArray, step, timestep);
swapAllVectorFields(simArray, timestep);
advectVectorsAcrossBoundaries(simArray, timestep);
solveProjection(simArray, step, timestep);
//queue all chunks
for(int x = 0; x < simArray.length; x++){
for(int y = 0; y < simArray[0].length; y++){
for(int z = 0; z < simArray[0][0].length; z++){
queueChunkWrapper(simArray[x][y][z]);
}
}
}
//
//Density stage
addDensity(simArray, timestep);
swapAllDensityArrays(simArray, timestep);
diffuseDensity(simArray, timestep);
swapAllDensityArrays(simArray, timestep);
advectDensity(simArray, timestep);
//Vector stage
simulateWrapper(timestep);
// solveChunkMask(simArray);
// addVectorSources(simArray, timestep);
// swapAllVectorFields(simArray, timestep);
// solveVectorDiffusion(simArray, timestep);
// solveProjection(simArray, step, timestep);
// swapAllVectorFields(simArray, timestep);
// advectVectorsAcrossBoundaries(simArray, timestep);
// solveProjection(simArray, step, timestep);
// //
// //Density stage
// addDensity(simArray, timestep);
// swapAllDensityArrays(simArray, timestep);
// diffuseDensity(simArray, timestep);
// swapAllDensityArrays(simArray, timestep);
// advectDensity(simArray, timestep);
// mirrorNeighborDensities(simArray, timestep);
@ -715,38 +725,38 @@ public class FluidSim {
/**
* The native function call to simulate a frame of fluid
* @param DIM_X
* @param DIM_Y
* @param DIM_Z
* @param x
* @param x0
* @param u
* @param v
* @param w
* @param u0
* @param v0
* @param w0
* @param DIFFUSION_CONSTANT
* @param VISCOSITY_CONSTANT
* @param timestep
*/
private native void simulate(
int DIM_X,
int chunkMask,
ByteBuffer[] x,
ByteBuffer x0,
ByteBuffer[] u,
ByteBuffer[] v,
ByteBuffer[] w,
ByteBuffer[] u0,
ByteBuffer[] v0,
ByteBuffer[] w0,
float DIFFUSION_CONSTANT,
float VISCOSITY_CONSTANT,
float timestep
);
// /**
// * The native function call to simulate a frame of fluid
// * @param DIM_X
// * @param DIM_Y
// * @param DIM_Z
// * @param x
// * @param x0
// * @param u
// * @param v
// * @param w
// * @param u0
// * @param v0
// * @param w0
// * @param DIFFUSION_CONSTANT
// * @param VISCOSITY_CONSTANT
// * @param timestep
// */
// private native void simulate(
// int DIM_X,
// int chunkMask,
// ByteBuffer[] x,
// ByteBuffer x0,
// ByteBuffer[] u,
// ByteBuffer[] v,
// ByteBuffer[] w,
// ByteBuffer[] u0,
// ByteBuffer[] v0,
// ByteBuffer[] w0,
// float DIFFUSION_CONSTANT,
// float VISCOSITY_CONSTANT,
// float timestep
// );
private void calculateChunkMaskWrapper(){
this.chunkMask = this.calculateChunkMask(density);
@ -852,6 +862,57 @@ public class FluidSim {
/**
* Queues a chunk
* @param chunk the chunk
*/
private static void queueChunkWrapper(FluidSim chunk){
queueChunk(
DIM,
chunk.chunkMask,
chunk.density,
chunk.uVector,
chunk.vVector,
chunk.wVector,
chunk.densityAddition,
chunk.uAdditionVector,
chunk.vAdditionVector,
chunk.wAdditionVector,
DIFFUSION_CONSTANT,
VISCOSITY_CONSTANT
);
}
private static native void queueChunk(
int DIM_X,
int chunkMask,
ByteBuffer d[],
ByteBuffer u[],
ByteBuffer v[],
ByteBuffer w[],
ByteBuffer d0[],
ByteBuffer u0[],
ByteBuffer v0[],
ByteBuffer w0[],
float DIFFUSION_CONSTANT,
float VISCOSITY_CONSTANT
);
/**
* Main simulation function
* @param timestep
*/
private static void simulateWrapper(float timestep){
simulate(timestep);
}
private static native void simulate(float timestep);