Skip to content

Commit 614db50

Browse files
committed
making vectorforth independent from TBB
1 parent 023f98b commit 614db50

File tree

10 files changed

+98
-29
lines changed

10 files changed

+98
-29
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "jtk"]
2+
path = jtk
3+
url = https://github.com/janm31415/jtk

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")
99
set(CMAKE_CXX_STANDARD 17)
1010
set(CMAKE_CXX_STANDARD_REQUIRED ON)
1111

12+
set(JTK_THREADING "std" CACHE STRING "Choose your threading library: tbb is Intel's Threading Building Blocks library, ppl is Windows' concurrency library, std uses std::thread, and none does not use threading.")
13+
14+
set_property(CACHE JTK_THREADING PROPERTY STRINGS tbb ppl std none)
15+
1216
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
1317

1418
set(AVX512 OFF CACHE BOOL "Set ON for AVX-512, set OFF for AVX2.")
@@ -21,6 +25,7 @@ message(STATUS "SINCOS_METHOD='${SINCOS_METHOD}'")
2125

2226
add_subdirectory(asm)
2327
add_subdirectory(asm.tests)
28+
add_subdirectory(jtk)
2429
add_subdirectory(sf)
2530
add_subdirectory(shaderforth)
2631
add_subdirectory(vectorforth)

jtk

Submodule jtk added at 334015e

sf/CMakeLists.txt

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ if (UNIX)
1010
set(OPENGL_LIBRARIES "/usr/lib/x86_64-linux-gnu/libOpenGL.so.0.0.0")
1111
endif (UNIX)
1212

13+
if (APPLE)
14+
set(TBB_INCLUDE_DIR "")
15+
set(TBB_LIBRARIES "")
16+
set(OPENGL_LIBRARIES "/System/Library/Frameworks/OpenGL.framework/OpenGL")
17+
endif (APPLE)
18+
1319
set(HDRS
1420
window.h
1521
)
@@ -50,17 +56,30 @@ if (${SINCOS_METHOD} STREQUAL "approximate")
5056
add_definitions(-DSINCOSAPPROX)
5157
endif (${SINCOS_METHOD} STREQUAL "approximate")
5258

59+
if (${JTK_THREADING} STREQUAL "tbb")
60+
add_definitions(-D_ENABLE_TBB)
61+
endif (${JTK_THREADING} STREQUAL "tbb")
62+
63+
if (${JTK_THREADING} STREQUAL "ppl")
64+
add_definitions(-D_ENABLE_PPL)
65+
endif (${JTK_THREADING} STREQUAL "ppl")
66+
67+
if (${JTK_THREADING} STREQUAL "std")
68+
add_definitions(-D_ENABLE_THREADS)
69+
endif (${JTK_THREADING} STREQUAL "std")
70+
5371
add_executable(sf ${HDRS} ${SRCS})
5472
source_group("Header Files" FILES ${hdrs})
5573
source_group("Source Files" FILES ${srcs})
56-
74+
75+
if (WIN32)
5776
target_include_directories(sf
5877
PRIVATE
5978
${CMAKE_CURRENT_SOURCE_DIR}/../
79+
${CMAKE_CURRENT_SOURCE_DIR}/../jtk/
6080
${TBB_INCLUDE_DIR}
6181
)
62-
63-
if (WIN32)
82+
6483
target_link_libraries(sf
6584
PRIVATE
6685
asm
@@ -70,11 +89,28 @@ target_link_libraries(sf
7089
endif (WIN32)
7190

7291
if (UNIX)
92+
93+
set(XLIBINCLUDE "")
94+
set(XLIBLIBRARY "X11.so")
95+
96+
if (APPLE)
97+
set(XLIBINCLUDE "/usr/X11/include/")
98+
set(XLIBLIBRARY "/usr/X11/lib/libX11.dylib")
99+
endif (APPLE)
100+
101+
target_link_libraries(sf
102+
PRIVATE
103+
asm
104+
vectorforth
105+
${TBB_LIBRARIES}
106+
${XLIBINCLUDE}
107+
)
108+
73109
target_link_libraries(sf
74110
PRIVATE
75111
asm
76112
vectorforth
77-
X11.so
113+
${XLIBLIBRARY}
78114
${TBB_LIBRARIES}
79115
)
80116
endif (UNIX)

sf/main.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
#include <vectorforth/stdlib.h>
1717
#include <vectorforth/sincos_table.h>
1818

19-
#include <tbb/parallel_for.h>
20-
#include <tbb/enumerable_thread_specific.h>
19+
#include <jtk/concurrency.h>
2120

2221
#include "window.h"
2322

@@ -282,7 +281,7 @@ int main(int argc, char** argv)
282281
int frame = 0;
283282
auto last_tic = std::chrono::high_resolution_clock::now();
284283
float time = 0.f;
285-
tbb::enumerable_thread_specific< VF::context > local_context;
284+
jtk::combinable< VF::context > local_context;
286285

287286
while (!l.quit)
288287
{
@@ -318,7 +317,7 @@ int main(int argc, char** argv)
318317
#ifdef SINGLE
319318
for (int y = 0; y < h; ++y)
320319
#else
321-
tbb::parallel_for((int)0, h, [&](int y)
320+
jtk::parallel_for((int)0, h, [&](int y)
322321
#endif
323322
{
324323

@@ -463,10 +462,11 @@ int main(int argc, char** argv)
463462

464463
_mm_free(image);
465464

466-
for (auto& ctxt : local_context)
465+
local_context.combine_each([](VF::context& ctxt)
467466
{
468467
VF::destroy_context(ctxt);
469-
}
468+
});
469+
470470
ASM::free_assembled_function((void*)fun, fun_size);
471471

472472
printf("\n");

shaderforth/CMakeLists.txt

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ if (UNIX)
1414
set(OPENGL_LIBRARIES "/usr/lib/x86_64-linux-gnu/libOpenGL.so.0.0.0")
1515
endif (UNIX)
1616

17+
if (APPLE)
18+
set(SDL_INCLUDE_DIR "/Library/Frameworks/SDL2.framework/Headers/")
19+
set(SDL_LIBRARIES "/Library/Frameworks/SDL2.framework/SDL2")
20+
set(TBB_INCLUDE_DIR "")
21+
set(TBB_LIBRARIES "")
22+
set(OPENGL_LIBRARIES "/System/Library/Frameworks/OpenGL.framework/OpenGL")
23+
endif (APPLE)
24+
1725
set(IMGUI
1826
${CMAKE_CURRENT_SOURCE_DIR}/../thirdparty/imgui_sdl/imgui.h
1927
${CMAKE_CURRENT_SOURCE_DIR}/../thirdparty/imgui_sdl/imgui_impl_opengl2.h
@@ -62,10 +70,10 @@ set(CMAKE_CXX_FLAGS_RELEASE "/W4 /MP /GF /O2 /Ob2 /Oi /Ot /MD /Zi")
6270
endif (WIN32)
6371

6472
if (UNIX)
65-
set(CMAKE_C_FLAGS_DEBUG "-mavx2 -mfma")
66-
set(CMAKE_CXX_FLAGS_DEBUG "-mavx2 -mfma")
67-
set(CMAKE_C_FLAGS_RELEASE "-mavx2 -mfma")
68-
set(CMAKE_CXX_FLAGS_RELEASE "-mavx2 -mfma")
73+
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -mavx2 -mfma -pthread")
74+
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -mavx2 -mfma -pthread")
75+
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -mavx2 -mfma -pthread")
76+
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -mavx2 -mfma -pthread")
6977
endif (UNIX)
7078

7179
# general build definitions
@@ -84,6 +92,18 @@ if (${SINCOS_METHOD} STREQUAL "approximate")
8492
add_definitions(-DSINCOSAPPROX)
8593
endif (${SINCOS_METHOD} STREQUAL "approximate")
8694

95+
if (${JTK_THREADING} STREQUAL "tbb")
96+
add_definitions(-D_ENABLE_TBB)
97+
endif (${JTK_THREADING} STREQUAL "tbb")
98+
99+
if (${JTK_THREADING} STREQUAL "ppl")
100+
add_definitions(-D_ENABLE_PPL)
101+
endif (${JTK_THREADING} STREQUAL "ppl")
102+
103+
if (${JTK_THREADING} STREQUAL "std")
104+
add_definitions(-D_ENABLE_THREADS)
105+
endif (${JTK_THREADING} STREQUAL "std")
106+
87107
if (WIN32)
88108
add_executable(shaderforth WIN32 ${HDRS} ${SRCS} ${IMGUI} ${JSON})
89109
endif (WIN32)
@@ -100,6 +120,7 @@ source_group("ThirdParty/json" FILES ${JSON})
100120
target_include_directories(shaderforth
101121
PRIVATE
102122
${CMAKE_CURRENT_SOURCE_DIR}/../
123+
${CMAKE_CURRENT_SOURCE_DIR}/../jtk/
103124
${CMAKE_CURRENT_SOURCE_DIR}/../thirdparty/imgui_sdl/
104125
${CMAKE_CURRENT_SOURCE_DIR}/../thirdparty/json/
105126
${SDL_INCLUDE_DIR}

shaderforth/shader_program.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
#include <sstream>
77

8-
#include <tbb/parallel_for.h>
9-
#include <tbb/enumerable_thread_specific.h>
8+
#include <jtk/concurrency.h>
109

1110
#include <vectorforth/context.h>
1211
#include <vectorforth/compiler.h>
@@ -98,10 +97,10 @@ shader_program::shader_program(int w, int h) : _w(w), _h(h), _fun_size(0), _fun(
9897

9998
shader_program::~shader_program()
10099
{
101-
for (auto& ctxt : local_context)
100+
local_context.combine_each([](VF::context& ctxt)
102101
{
103102
VF::destroy_context(ctxt);
104-
}
103+
});
105104
if (_fun)
106105
ASM::free_assembled_function((void*)_fun, _fun_size);
107106
}
@@ -233,7 +232,7 @@ void shader_program::run(image<uint32_t>& im)
233232
__m256 global_time_val = _mm256_set1_ps(_input.global_time);
234233
#endif
235234

236-
tbb::parallel_for((int)0, _h, [&](int y)
235+
jtk::parallel_for((int)0, _h, [&](int y)
237236
{
238237

239238
uint32_t* p_im = im.data() + y * _w;

shaderforth/shader_program.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
#include <string>
77
#include <stdint.h>
88

9-
#include <tbb/enumerable_thread_specific.h>
9+
#ifdef _WIN32
10+
#include <windows.h>
11+
#endif
12+
13+
#include <jtk/concurrency.h>
1014

1115
#include <vectorforth/context.h>
1216

@@ -33,5 +37,5 @@ class shader_program
3337
typedef void(*fun_ptr)(void*);
3438
fun_ptr _fun;
3539

36-
tbb::enumerable_thread_specific< VF::context > local_context;
40+
jtk::combinable< VF::context > local_context;
3741
};

vectorforth.tests/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ set(CMAKE_CXX_FLAGS_RELEASE "/W4 /MP /GF /O2 /Ob2 /Oi /Ot /MD /Zi /DNDEBUG")
2626
endif (WIN32)
2727

2828
if (UNIX)
29-
set(CMAKE_C_FLAGS_DEBUG "-mavx2 -mfma")
30-
set(CMAKE_CXX_FLAGS_DEBUG "-mavx2 -mfma")
31-
set(CMAKE_C_FLAGS_RELEASE "-mavx2 -mfma")
32-
set(CMAKE_CXX_FLAGS_RELEASE "-mavx2 -mfma")
29+
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -mavx2 -mfma")
30+
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -mavx2 -mfma")
31+
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -mavx2 -mfma")
32+
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -mavx2 -mfma")
3333
endif (UNIX)
3434

3535
# general build definitions

vectorforth/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ set(CMAKE_CXX_FLAGS_RELEASE "/W4 /MP /GF /O2 /Ob2 /Oi /Ot /MD /Zi /DNDEBUG /arch
4848
endif (WIN32)
4949

5050
if (UNIX)
51-
set(CMAKE_C_FLAGS_DEBUG "-mavx2 -mfma")
52-
set(CMAKE_CXX_FLAGS_DEBUG "-mavx2 -mfma")
53-
set(CMAKE_C_FLAGS_RELEASE "-mavx2 -mfma")
54-
set(CMAKE_CXX_FLAGS_RELEASE "-mavx2 -mfma")
51+
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -mavx2 -mfma")
52+
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -mavx2 -mfma")
53+
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -mavx2 -mfma")
54+
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -mavx2 -mfma")
5555
endif (UNIX)
5656

5757
# general build definitions

0 commit comments

Comments
 (0)