|
4 | 4 |
|
5 | 5 | #include <hicr/backends/hwloc/topologyManager.hpp> |
6 | 6 | #include <hicr/backends/pthreads/instanceManager.hpp> |
7 | | -#include <hicr/backends/pthreads/instancePool.hpp> |
8 | 7 |
|
9 | 8 | #include "../include/createInstance.hpp" |
10 | 9 |
|
11 | 10 | int main(int argc, char const *argv[]) |
12 | 11 | { |
13 | 12 | // Check argvs |
14 | | - if (argc != 3) { HICR_THROW_RUNTIME("Pass the instance count as argument"); } |
| 13 | + if (argc != 2) { HICR_THROW_RUNTIME("Pass the number of instances to create as argument"); } |
15 | 14 |
|
16 | 15 | // Get instance count |
17 | | - size_t instanceCount = std::atoi(argv[1]); |
18 | | - size_t instanceToCreate = std::atoi(argv[2]); |
| 16 | + size_t instancesToCreate = std::atoi(argv[1]); |
19 | 17 |
|
20 | 18 | // Determine the root instance id |
21 | 19 | HiCR::Instance::instanceId_t rootInstanceId = pthread_self(); |
22 | 20 |
|
23 | | - // Create Instance pool |
24 | | - HiCR::backend::pthreads::InstancePool instancePool(0); |
25 | | - |
26 | 21 | // Declare entrypoint |
27 | | - auto entrypoint = [&](HiCR::backend::pthreads::InstanceManager *creatorIm) { |
28 | | - auto im = HiCR::backend::pthreads::InstanceManager(rootInstanceId, creatorIm->getEntrypoint(), instancePool); |
29 | | - printf("[Instance %lu] Hello World\n", im.getCurrentInstance()->getId()); |
30 | | - }; |
31 | | - |
32 | | - // Define initial threads function |
33 | | - auto workload = [&]() { |
34 | | - // Create instance manager |
35 | | - auto im = HiCR::backend::pthreads::InstanceManager(rootInstanceId, entrypoint, instancePool); |
36 | | - |
37 | | - // Detect already started instances |
38 | | - im.detectInstances(instanceCount); |
| 22 | + auto entrypoint = [&](HiCR::InstanceManager *parentInstanceManager) { |
| 23 | + // Cast to pthread instance manager |
| 24 | + auto p = dynamic_cast<HiCR::backend::pthreads::InstanceManager *>(parentInstanceManager); |
39 | 25 |
|
40 | | - // Discover local topology |
41 | | - auto tm = HiCR::backend::hwloc::TopologyManager::createDefault(); |
42 | | - auto t = tm->queryTopology(); |
| 26 | + // Fail if the casting is not successful |
| 27 | + if (p == nullptr) { HICR_THROW_RUNTIME("Can not cast instance manager to a pthread-specific one"); } |
43 | 28 |
|
44 | | - // Create the new instance |
45 | | - createInstances(im, instanceToCreate, t); |
| 29 | + // Create instance manager |
| 30 | + auto createdInstanceManager = HiCR::backend::pthreads::InstanceManager(rootInstanceId, p->getEntrypoint()); |
46 | 31 |
|
47 | | - // Finalize instance manager |
48 | | - im.finalize(); |
| 32 | + // Run worker function |
| 33 | + workerFc(createdInstanceManager); |
49 | 34 | }; |
50 | 35 |
|
51 | | - std::vector<std::unique_ptr<std::thread>> initialThreads; |
52 | | - for (size_t i = 0; i < instanceCount - 1; i++) |
53 | | - { |
54 | | - // Create thread running the workload |
55 | | - auto thread = std::make_unique<std::thread>(workload); |
| 36 | + // Create instance manager |
| 37 | + auto instanceManager = HiCR::backend::pthreads::InstanceManager(rootInstanceId, entrypoint); |
56 | 38 |
|
57 | | - // Add to the vector of initial threads |
58 | | - initialThreads.push_back(std::move(thread)); |
59 | | - } |
| 39 | + // Discover local topology |
| 40 | + auto topologyManager = HiCR::backend::hwloc::TopologyManager::createDefault(); |
| 41 | + auto topology = topologyManager->queryTopology(); |
60 | 42 |
|
61 | | - // Run the workload |
62 | | - workload(); |
| 43 | + // Create the new instance |
| 44 | + createInstances(instanceManager, instancesToCreate, topology); |
63 | 45 |
|
64 | | - // Wait for all the threads to join |
65 | | - for (auto &thread : initialThreads) { thread->join(); } |
| 46 | + // Finalize instance manager |
| 47 | + instanceManager.finalize(); |
66 | 48 |
|
67 | 49 | printf("Terminating execution\n"); |
68 | 50 |
|
|
0 commit comments