-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(itk-wasm): add pthreads and cxx-threads test pipelines
- Loading branch information
Showing
5 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
packages/core/typescript/itk-wasm/test/pipelines/cxx-threads-pipeline/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
cmake_minimum_required(VERSION 3.16) | ||
project(cxx-threads-test) | ||
|
||
set(CMAKE_CXX_STANDARD 20) | ||
|
||
find_package(ITK REQUIRED | ||
COMPONENTS WebAssemblyInterface) | ||
include(${ITK_USE_FILE}) | ||
add_executable(cxx-threads-test cxx-threads-test.cxx) | ||
target_link_libraries(cxx-threads-test PUBLIC WebAssemblyInterface) | ||
|
||
enable_testing() | ||
add_test(NAME CjXThreadsTest | ||
COMMAND cxx-threads-test | ||
${CMAKE_CURRENT_BINARY_DIR}/cxx-threads-test-output.json | ||
) |
61 changes: 61 additions & 0 deletions
61
packages/core/typescript/itk-wasm/test/pipelines/cxx-threads-pipeline/cxx-threads-test.cxx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/*========================================================================= | ||
* | ||
* Copyright NumFOCUS | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0.txt | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*=========================================================================*/ | ||
#include "itkPipeline.h" | ||
#include "itkOutputTextStream.h" | ||
|
||
#include <sstream> | ||
#include <iostream> | ||
#include <atomic> | ||
#include <thread> | ||
#include <vector> | ||
|
||
static std::atomic<int> createdThreads(0); | ||
|
||
int main(int argc, char * argv[]) | ||
{ | ||
itk::wasm::Pipeline pipeline("cxx-threads-test", "A pipeline to test C++11 threads support", argc, argv); | ||
|
||
int numberOfThreads = 4; | ||
pipeline.add_option("-n,--number-of-threads", numberOfThreads, "The number of threads to use"); | ||
|
||
itk::wasm::OutputTextStream outputJson; | ||
pipeline.add_option("output-json", outputJson, "The output json with test results")->type_name("OUTPUT_JSON"); | ||
|
||
ITK_WASM_PARSE(pipeline); | ||
|
||
std::vector<std::thread> threads; | ||
threads.reserve(numberOfThreads); | ||
createdThreads = 0; | ||
|
||
for (int i = 0; i < numberOfThreads; ++i) | ||
{ | ||
threads.emplace_back([i]() | ||
{ | ||
std::cout << " in thread " << i << std::endl; | ||
createdThreads++; | ||
}); | ||
} | ||
|
||
for (auto &t : threads) | ||
{ | ||
t.join(); | ||
} | ||
|
||
outputJson.Get() << "{ \"createdThreads\": " << createdThreads << " }" << std::endl; | ||
return EXIT_SUCCESS; | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/core/typescript/itk-wasm/test/pipelines/pthreads-pipeline/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
cmake_minimum_required(VERSION 3.16) | ||
project(pthreads-test) | ||
|
||
set(CMAKE_CXX_STANDARD 20) | ||
|
||
find_package(ITK REQUIRED | ||
COMPONENTS WebAssemblyInterface) | ||
include(${ITK_USE_FILE}) | ||
add_executable(pthreads-test pthreads-test.cxx) | ||
target_link_libraries(pthreads-test PUBLIC WebAssemblyInterface) | ||
|
||
enable_testing() | ||
add_test(NAME PThreadsTest | ||
COMMAND pthreads-test | ||
${CMAKE_CURRENT_BINARY_DIR}/pthreads-test-output.json | ||
) |
68 changes: 68 additions & 0 deletions
68
packages/core/typescript/itk-wasm/test/pipelines/pthreads-pipeline/pthreads-test.cxx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/*========================================================================= | ||
* | ||
* Copyright NumFOCUS | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0.txt | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*=========================================================================*/ | ||
#include "itkPipeline.h" | ||
#include "itkOutputTextStream.h" | ||
|
||
#include <sstream> | ||
#include <iostream> | ||
#include <atomic> | ||
|
||
#include "pthread.h" | ||
|
||
static std::atomic<int> createdThreads(0); | ||
|
||
void *thread_entry_point(void *ctx) { | ||
int id = (int) ctx; | ||
printf(" in thread %d\n", id); | ||
createdThreads++; | ||
return 0; | ||
} | ||
|
||
int main( int argc, char * argv[] ) | ||
{ | ||
itk::wasm::Pipeline pipeline("pthreads-test", "A pipeline to test pthreads support", argc, argv); | ||
|
||
int numberOfThreads = 4; | ||
pipeline.add_option("-n,--number-of-threads", numberOfThreads, "The number of threads to use"); | ||
|
||
itk::wasm::OutputTextStream outputJson; | ||
pipeline.add_option("output-json", outputJson, "The output json with test results")->type_name("OUTPUT_JSON"); | ||
|
||
ITK_WASM_PARSE(pipeline); | ||
|
||
// Based on: https://bytecodealliance.org/articles/wasi-threads | ||
pthread_t threads[256]; | ||
createdThreads = 0; | ||
for (int i = 0; i < numberOfThreads; i++) { | ||
int ret = pthread_create(&threads[i], NULL, &thread_entry_point, (void *) i); | ||
if (ret) { | ||
printf("failed to spawn thread: %s", strerror(ret)); | ||
} | ||
} | ||
|
||
// Wait for all threads to finish | ||
for (int i = 0; i < numberOfThreads; i++) | ||
{ | ||
pthread_join(threads[i], NULL); | ||
} | ||
|
||
// Write the output JSON | ||
outputJson.Get() << "{ \"createdThreads\": " << createdThreads << " }" << std::endl; | ||
|
||
return EXIT_SUCCESS; | ||
} |