Skip to content

Commit 9f7242f

Browse files
committed
feat: pthread instance manager supports only creation and not detection of new instances
1 parent ad41917 commit 9f7242f

File tree

5 files changed

+177
-310
lines changed

5 files changed

+177
-310
lines changed

examples/createInstance/include/createInstance.hpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,27 @@
44
#include <hicr/core/instanceManager.hpp>
55
#include <hicr/core/topology.hpp>
66

7-
void createInstances(HiCR::InstanceManager &im, size_t instanceCount, HiCR::Topology &t)
7+
/**
8+
* Create new HiCR instances
9+
*
10+
* \param[in] instanceManager
11+
* \param[in] instanceCount
12+
* \param[in] topology
13+
*/
14+
void createInstances(HiCR::InstanceManager &instanceManager, size_t instanceCount, HiCR::Topology &topology)
815
{
9-
auto instanceTemplate = im.createInstanceTemplate(t);
16+
auto instanceTemplate = instanceManager.createInstanceTemplate(topology);
1017

1118
for (size_t i = 0; i < instanceCount; i++)
1219
{
13-
auto instance = im.createInstance(*instanceTemplate);
14-
printf("[Instance %lu] Create instance %lu\n", im.getCurrentInstance()->getId(), instance->getId());
20+
auto instance = instanceManager.createInstance(*instanceTemplate);
21+
printf("[Instance %lu] Create instance %lu\n", instanceManager.getCurrentInstance()->getId(), instance->getId());
1522
}
16-
}
23+
}
24+
25+
/**
26+
* Function that all the created instances should execute
27+
*
28+
* \param[in] instanceManager
29+
*/
30+
void workerFc(HiCR::InstanceManager &instanceManager) { printf("[Instance %lu] Hello World\n", instanceManager.getCurrentInstance()->getId()); }

examples/createInstance/source/pthreads.cpp

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,47 @@
44

55
#include <hicr/backends/hwloc/topologyManager.hpp>
66
#include <hicr/backends/pthreads/instanceManager.hpp>
7-
#include <hicr/backends/pthreads/instancePool.hpp>
87

98
#include "../include/createInstance.hpp"
109

1110
int main(int argc, char const *argv[])
1211
{
1312
// 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"); }
1514

1615
// 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]);
1917

2018
// Determine the root instance id
2119
HiCR::Instance::instanceId_t rootInstanceId = pthread_self();
2220

23-
// Create Instance pool
24-
HiCR::backend::pthreads::InstancePool instancePool(0);
25-
2621
// 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);
3925

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"); }
4328

44-
// Create the new instance
45-
createInstances(im, instanceToCreate, t);
29+
// Create instance manager
30+
auto createdInstanceManager = HiCR::backend::pthreads::InstanceManager(rootInstanceId, p->getEntrypoint());
4631

47-
// Finalize instance manager
48-
im.finalize();
32+
// Run worker function
33+
workerFc(createdInstanceManager);
4934
};
5035

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);
5638

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();
6042

61-
// Run the workload
62-
workload();
43+
// Create the new instance
44+
createInstances(instanceManager, instancesToCreate, topology);
6345

64-
// Wait for all the threads to join
65-
for (auto &thread : initialThreads) { thread->join(); }
46+
// Finalize instance manager
47+
instanceManager.finalize();
6648

6749
printf("Terminating execution\n");
6850

include/hicr/backends/pthreads/instance.hpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
/*
2+
* Copyright 2025 Huawei Technologies Co., Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* @file instance.hpp
19+
* @brief This file implements the instance class for the Pthreads backend
20+
* @author L. Terracciano
21+
* @date 10/10/2025
22+
*/
23+
124
#pragma once
225

326
#include <pthread.h>
@@ -7,12 +30,21 @@
730
namespace HiCR::backend::pthreads
831
{
932

33+
/**
34+
* Implementation of the HiCR Instance
35+
*/
1036
class Instance : public HiCR::Instance
1137
{
1238
public:
1339

14-
Instance(instanceId_t currentInstanceId, instanceId_t rootInstanceId)
15-
: HiCR::Instance(currentInstanceId),
40+
/**
41+
* Constructor
42+
*
43+
* \param[in] instanceId the id of the instance
44+
* \param[in] rootInstanceId the id of root
45+
*/
46+
Instance(instanceId_t instanceId, instanceId_t rootInstanceId)
47+
: HiCR::Instance(instanceId),
1648
_rootInstanceId(rootInstanceId){};
1749

1850
~Instance() = default;
@@ -21,6 +53,9 @@ class Instance : public HiCR::Instance
2153

2254
private:
2355

56+
/**
57+
* Id of HiCR root instance
58+
*/
2459
instanceId_t _rootInstanceId;
2560
};
2661
} // namespace HiCR::backend::pthreads

0 commit comments

Comments
 (0)