Skip to content

Commit

Permalink
Fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
koparasy committed Jun 10, 2024
1 parent 636747d commit e448ed5
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 66 deletions.
6 changes: 4 additions & 2 deletions src/AMSlib/AMS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,9 @@ class AMSWrap
} else if (log_prefix.find("<PID>") != std::string::npos) {
pattern = std::string("<PID>");
id = getpid();
} // Combine hostname and pid
}

// Combine hostname and pid
std::ostringstream combined;
combined << "." << hostname << "." << id;

Expand Down Expand Up @@ -665,7 +666,8 @@ const char *AMSGetAllocatorName(AMSResourceType device)
void AMSSetAllocator(AMSResourceType resource, const char *alloc_name)
{
auto &rm = ams::ResourceManager::getInstance();
rm.setAllocator(std::string(alloc_name), resource);
std::string alloc(alloc_name);
rm.setAllocator(alloc, resource);
}

AMSCAbstrModel AMSRegisterAbstractModel(const char *domain_name,
Expand Down
16 changes: 12 additions & 4 deletions src/AMSlib/wf/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "AMS.h"
#include "wf/debug.h"

#define UNDEFINED_FUNC -1

#ifdef __ENABLE_CUDA__
namespace ams
{
Expand Down Expand Up @@ -254,6 +256,7 @@ inline void computePredicate(float *data,
const size_t kneigh,
float threshold)
{
FATAL(Device, "Called device code when CUDA disabled");
return;
}

Expand All @@ -265,6 +268,7 @@ inline void linearize(TypeOutValue *output,
size_t dims,
size_t elements)
{
FATAL(Device, "Called device code when CUDA disabled");
return;
}

Expand All @@ -277,7 +281,8 @@ inline int pack(bool cond,
TypeValue **dense,
int dims)
{
return -1;
FATAL(Device, "Called device code when CUDA disabled");
return UNDEFINED_FUNC;
}

template <typename TypeValue>
Expand All @@ -290,7 +295,8 @@ inline int pack(bool cond,
int *sparse_indices,
int dims)
{
return -1;
FATAL(Device, "Called device code when CUDA disabled");
return UNDEFINED_FUNC;
}

template <typename TypeValue>
Expand All @@ -302,7 +308,8 @@ inline int unpack(bool cond,
TypeValue **dense,
int dims)
{
return -1;
FATAL(Device, "Called device code when CUDA disabled");
return UNDEFINED_FUNC;
}

template <typename TypeValue>
Expand All @@ -314,7 +321,8 @@ inline int unpack(bool cond,
int *sparse_indices,
int dims)
{
return -1;
FATAL(Device, "Called device code when CUDA disabled");
return UNDEFINED_FUNC;
}

} // namespace Device
Expand Down
102 changes: 53 additions & 49 deletions src/AMSlib/wf/resource_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

#include <cstdlib>
#include <cstring>
#include <umpire/ResourceManager.hpp>
#include <umpire/Umpire.hpp>
#include <umpire/strategy/QuickPool.hpp>

#include "debug.h"
#include "device.hpp"
Expand All @@ -18,13 +15,13 @@
namespace ams
{

template<typename T>
static T roundUp(T num_to_round, int multiple)
template <typename T>
static T roundUp(T num_to_round, int multiple)
{
return ((num_to_round + multiple- 1) / multiple) * multiple;
return ((num_to_round + multiple - 1) / multiple) * multiple;
}

std::string AMSAllocator::getName() { return name; }
const std::string AMSAllocator::getName() const { return name; }


struct AMSDefaultDeviceAllocator final : AMSAllocator {
Expand All @@ -40,7 +37,10 @@ struct AMSDefaultHostAllocator final : AMSAllocator {
AMSDefaultHostAllocator(std::string name) : AMSAllocator(name) {}
~AMSDefaultHostAllocator() = default;

void *allocate(size_t num_bytes) { return aligned_alloc(8, roundUp(num_bytes, 8)); }
void *allocate(size_t num_bytes)
{
return aligned_alloc(8, roundUp(num_bytes, 8));
}

void deallocate(void *ptr) { free(ptr); }
};
Expand All @@ -63,51 +63,55 @@ void _raw_copy(void *src,
AMSResourceType dest_dev,
size_t num_bytes)
{
if (src_dev == AMSResourceType::AMS_HOST) {
if (dest_dev == AMSResourceType::AMS_HOST) {
std::memcpy(dest, src, num_bytes);
} else if (dest_dev == AMSResourceType::AMS_DEVICE) {
HtoDMemcpy(dest, src, num_bytes);
} else if (dest_dev == AMSResourceType::AMS_PINNED) {
std::memcpy(dest, src, num_bytes);
} else {
FATAL(AMSResource, "Unknown copy dest")
}
} else if (src_dev == AMSResourceType::AMS_DEVICE) {
if (dest_dev == AMSResourceType::AMS_HOST) {
DtoHMemcpy(dest, src, num_bytes);
} else if (dest_dev == AMSResourceType::AMS_DEVICE) {
DtoDMemcpy(dest, src, num_bytes);
} else if (dest_dev == AMSResourceType::AMS_PINNED) {
DtoHMemcpy(dest, src, num_bytes);
} else {
FATAL(AMSResource, "Unknown copy dest")
}
} else if (src_dev == AMSResourceType::AMS_PINNED) {
if (dest_dev == AMSResourceType::AMS_HOST) {
std::memcpy(dest, src, num_bytes);
} else if (dest_dev == AMSResourceType::AMS_DEVICE) {
HtoDMemcpy(dest, src, num_bytes);
} else if (dest_dev == AMSResourceType::AMS_PINNED) {
std::memcpy(dest, src, num_bytes);
} else {
FATAL(AMSResource, "Unknown copy dest")
}
switch (src_dev) {
case AMSResourceType::AMS_HOST:
case AMSResourceType::AMS_PINNED:
switch (dest_dev) {
case AMSResourceType::AMS_HOST:
case AMSResourceType::AMS_PINNED:
std::memcpy(dest, src, num_bytes);
break;
case AMSResourceType::AMS_DEVICE:
HtoDMemcpy(dest, src, num_bytes);
break;
default:
FATAL(ResourceManager, "Unknown device type to copy to from HOST");
break;
}
break;
case AMSResourceType::AMS_DEVICE:
switch (dest_dev) {
case AMSResourceType::AMS_DEVICE:
DtoDMemcpy(dest, src, num_bytes);
break;
case AMSResourceType::AMS_HOST:
case AMSResourceType::AMS_PINNED:
DtoHMemcpy(dest, src, num_bytes);
break;
default:
FATAL(ResourceManager, "Unknown device type to copy to from DEVICE");
break;
}
default:
FATAL(ResourceManager, "Unknown device type to copy from");
}
}

AMSAllocator *_get_allocator(std::string alloc_name, AMSResourceType resource)
AMSAllocator *_get_allocator(std::string &alloc_name, AMSResourceType resource)
{
if (resource == AMSResourceType::AMS_DEVICE) {
return new AMSDefaultDeviceAllocator(alloc_name);
} else if (resource == AMSResourceType::AMS_HOST) {
return new AMSDefaultHostAllocator(alloc_name);
} else if (resource == AMSResourceType::AMS_PINNED) {
return new AMSDefaultPinnedAllocator(alloc_name);
} else {
FATAL(ResourceManager,
"Requested allocator %s for Unknown resource type",
alloc_name.c_str());
switch (resource) {
case AMSResourceType::AMS_DEVICE:
return new AMSDefaultDeviceAllocator(alloc_name);
break;
case AMSResourceType::AMS_HOST:
return new AMSDefaultHostAllocator(alloc_name);
break;
case AMSResourceType::AMS_PINNED:
return new AMSDefaultPinnedAllocator(alloc_name);
break;
default:
FATAL(ResourceManager,
"Unknown resource type to create an allocator for");
}
}

Expand Down
24 changes: 13 additions & 11 deletions src/AMSlib/wf/resource_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define __AMS_ALLOCATOR__

#include <cstddef>
#include <string>
#include <vector>

#include "AMS.h"
Expand All @@ -27,7 +28,7 @@ void _raw_copy(void* src,
AMSResourceType dest_dev,
size_t num_bytes);

AMSAllocator* _get_allocator(std::string alloc_name, AMSResourceType resource);
AMSAllocator* _get_allocator(std::string& alloc_name, AMSResourceType resource);
void _release_allocator(AMSAllocator* allocator);

} // namespace internal
Expand All @@ -40,16 +41,15 @@ void _release_allocator(AMSAllocator* allocator);

struct AMSAllocator {
std::string name;
AMSAllocator(std::string alloc_name) : name(alloc_name) {}
AMSAllocator(std::string& alloc_name) : name(alloc_name) {}
virtual ~AMSAllocator() = default;

virtual void* allocate(size_t num_bytes) = 0;
virtual void deallocate(void* ptr) = 0;

std::string getName();
const std::string getName() const;
};


class ResourceManager
{
private:
Expand Down Expand Up @@ -77,7 +77,7 @@ class ResourceManager
}

/** @brief return the name of an allocator */
std::string getAllocatorName(AMSResourceType resource)
const std::string getAllocatorName(AMSResourceType resource) const
{
return RMAllocators[resource]->getName();
}
Expand Down Expand Up @@ -113,7 +113,7 @@ class ResourceManager
* @tparam TypeInValue type of pointers
* @param[in] src Source memory pointer.
* @param[out] dest destination memory pointer.
* @param[in] size number of values to copy.
* @param[in] size number of values to copy (It performs a shallow copy of nested pointers).
* @return void.
*/
template <typename TypeInValue>
Expand Down Expand Up @@ -146,18 +146,21 @@ class ResourceManager
void init()
{
DBG(ResourceManager, "Initialization of allocators");
std::string host_alloc("HOST");
std::string device_alloc("DEVICE");
std::string pinned_alloc("PINNED");
if (!RMAllocators[AMSResourceType::AMS_HOST])
setAllocator("HOST", AMSResourceType::AMS_HOST);
setAllocator(host_alloc, AMSResourceType::AMS_HOST);
#ifdef __ENABLE_CUDA__
if (!RMAllocators[AMSResourceType::AMS_DEVICE])
setAllocator("DEVICE", AMSResourceType::AMS_DEVICE);
setAllocator(host_alloc, AMSResourceType::AMS_DEVICE);

if (!RMAllocators[AMSResourceType::AMS_PINNED])
setAllocator("PINNED", AMSResourceType::AMS_PINNED);
setAllocator(pinned_alloc, AMSResourceType::AMS_PINNED);
#endif
}

void setAllocator(std::string alloc_name, AMSResourceType resource)
void setAllocator(std::string& alloc_name, AMSResourceType resource)
{
if (RMAllocators[resource]) {
delete RMAllocators[resource];
Expand Down Expand Up @@ -194,7 +197,6 @@ class ResourceManager
//! ------------------------------------------------------------------------
};


} // namespace ams

#endif
1 change: 1 addition & 0 deletions tests/AMSlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ function(ADDTEST exe test_name)
endfunction()

# This test requires Allocate
# TODO: Include tests once we re-instate a pool
#BUILD_TEST(ams_allocator_test ams_allocate.cpp)
#ADDTEST(ams_allocator_test AMSAllocate)
BUILD_TEST(ams_packing_test cpu_packing_test.cpp AMSPack)
Expand Down

0 comments on commit e448ed5

Please sign in to comment.