Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to compare resources #148

Merged
merged 45 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
7126ad3
WIP resource comparison
kab163 Apr 2, 2024
567e7d3
latest changes
kab163 Apr 3, 2024
b74be70
updates, still doesn't work
kab163 Apr 4, 2024
564f28f
what do you know, it works now
kab163 Apr 12, 2024
2355ed4
cleaning up resource hpps
kab163 Apr 12, 2024
d50e0c6
add to test and fix typos
kab163 Apr 12, 2024
cadd0f7
compare -> operator==; const& arguments; Cuda & Host operator==.
mdavis36 Apr 15, 2024
000e485
Unused arg
mdavis36 Apr 15, 2024
116a67b
adding NOT equal operator and comparisons
kab163 Apr 15, 2024
104965d
adding comments
kab163 Apr 15, 2024
3611642
adding const for hip func
kab163 Apr 15, 2024
7f8a5f3
falsehoods should return false
kab163 Apr 15, 2024
6c82c96
constantly const-ing const funcs
kab163 Apr 15, 2024
45aca16
Merge branch 'feature/kab163/stream-compare' of https://github.com/LL…
kab163 Apr 15, 2024
8560d42
constantly confusing const
kab163 Apr 15, 2024
d6538a0
Update include/camp/resource/sycl.hpp
kab163 Apr 16, 2024
8eec19a
Apply suggestions from code review
kab163 Apr 16, 2024
9da0aab
Update cuda.hpp
kab163 Apr 16, 2024
2322002
adding comments and spacing
kab163 Apr 17, 2024
43e8e0d
using compare func instead of operator overload
kab163 Apr 17, 2024
64038f3
Update include/camp/resource.hpp
kab163 Apr 17, 2024
ae8cfee
Apply suggestions from code review
kab163 Apr 17, 2024
e81c48c
Update include/camp/resource/sycl.hpp
kab163 Apr 18, 2024
174e2f9
adding templated operator== to Resource
kab163 Apr 18, 2024
ba76eb5
Merge branch 'feature/kab163/stream-compare' of https://github.com/LL…
kab163 Apr 18, 2024
83c046d
adding generic and concrete resource comparison
kab163 Apr 18, 2024
132bcbe
Update include/camp/resource.hpp
kab163 Apr 18, 2024
73da2c0
reducing some repetition in test, adding comments
kab163 Apr 22, 2024
dfae06b
merging with branch
kab163 Apr 22, 2024
6117aab
adding a few more operator overloads
kab163 Apr 23, 2024
00ba5ea
Update test/resource.cpp
kab163 Apr 23, 2024
83ff020
cleaning up test
kab163 Apr 23, 2024
9fed960
Merge branch 'feature/kab163/stream-compare' of https://github.com/LL…
kab163 Apr 23, 2024
601c9cf
fixing up PR
kab163 Apr 23, 2024
10a0077
trying to appease MSVC and SYCL
kab163 Apr 25, 2024
452642e
trying to make compilers happy but that is hard
kab163 Apr 25, 2024
1fbc3a0
attempting to fix MSVC and sycl issues
kab163 May 1, 2024
03de19f
removing test that makes error
kab163 May 1, 2024
2441e4c
removing Host2
kab163 May 1, 2024
d464f0e
need to make resource constructor know it cant convert char* to resource
kab163 May 1, 2024
6b91cda
trying to make test work
kab163 May 2, 2024
558e6d0
blah
kab163 May 2, 2024
b1bae7f
blah blah blah
kab163 May 2, 2024
2718519
Removing some tests in Compare
kab163 May 2, 2024
7e18902
adding to resource test
kab163 May 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions include/camp/resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace resources
return result ? result->get() : nullptr;
}
template <typename T>
T get()
T get() const
{
auto result = dynamic_cast<ContextModel<T> *>(m_value.get());
if (result == nullptr) {
Expand Down Expand Up @@ -94,29 +94,65 @@ namespace resources
void wait_for(Event *e) { m_value->wait_for(e); }
void wait() { m_value->wait(); }

/*
* \brief Compares two Resources to see if they are equal. Two Resources are equal if they are
* on the same platform and they describe the same stream (i.e. CUDA, HIP) or queue (i.e. SYCL).
* (Note: This operator overload is on the generic Resource. Using the ContextModel's operator
* overload, we can call the specific resource's (i.e. T) operator== to compare streams or
* queues.)
*
* \return True or false depending on comparison with m_value if they are on the same platform.
kab163 marked this conversation as resolved.
Show resolved Hide resolved
*/
bool operator==(Resource const& r) const
{
if(get_platform() == r.get_platform()) {
return (m_value->compare(r));
}
return false;
}
/*
kab163 marked this conversation as resolved.
Show resolved Hide resolved
* \brief Compares two Resources to see if they are NOT equal. Two Resources are not equal
* if they are on separate platforms. Also, even if they are on the same platform, if they
* describe different streams (i.e. CUDA, HIP) or different queues (i.e. SYCL), then they
* are not equal.
*
* \return Negation of == operator.
*/
bool operator!=(Resource const& r) const
{
return !(*this == r);
}

private:
class ContextInterface
{
public:
virtual ~ContextInterface() {}
virtual Platform get_platform() const = 0;

virtual bool compare(Resource const& r) const = 0;

virtual void *allocate(size_t size, MemoryAccess ma = MemoryAccess::Device) = 0;
virtual void *calloc(size_t size, MemoryAccess ma = MemoryAccess::Device) = 0;
virtual void deallocate(void *p, MemoryAccess ma = MemoryAccess::Device) = 0;
virtual void memcpy(void *dst, const void *src, size_t size) = 0;
virtual void memset(void *p, int val, size_t size) = 0;

virtual Event get_event() = 0;
virtual Event get_event_erased() = 0;
virtual void wait_for(Event *e) = 0;
virtual void wait() = 0;
};

template <typename T>
class ContextModel : public ContextInterface
class ContextModel final : public ContextInterface
{
public:
ContextModel(T const &modelVal) : m_modelVal(modelVal) {}
Platform get_platform() const override { return m_modelVal.get_platform(); }

bool compare(Resource const& r) const override { return m_modelVal == r.get<T>(); }

void *allocate(size_t size, MemoryAccess ma = MemoryAccess::Device) override { return m_modelVal.template allocate<char>(size, ma); }
void *calloc(size_t size, MemoryAccess ma = MemoryAccess::Device) override { return m_modelVal.calloc(size, ma); }
void deallocate(void *p, MemoryAccess ma = MemoryAccess::Device) override { m_modelVal.deallocate(p, ma); }
Expand All @@ -128,13 +164,15 @@ namespace resources
{
m_modelVal.memset(p, val, size);
}

Event get_event() override { return m_modelVal.get_event_erased(); }
Event get_event_erased() override
{
return m_modelVal.get_event_erased();
}
void wait_for(Event *e) override { m_modelVal.wait_for(e); }
void wait() override { m_modelVal.wait(); }

T *get() { return &m_modelVal; }

private:
Expand Down
24 changes: 22 additions & 2 deletions include/camp/resource/cuda.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,28 @@ namespace resources
}
}

cudaStream_t get_stream() { return stream; }
int get_device() { return device; }
cudaStream_t get_stream() const { return stream; }
int get_device() const { return device; }

/*
* \brief Compares two (Cuda) resources to see if they are equal.
*
* \return True or false depending on if it is the same stream.
*/
bool operator==(Cuda const& c) const
{
return (get_stream() == c.get_stream());
}

/*
kab163 marked this conversation as resolved.
Show resolved Hide resolved
* \brief Compares two (Cuda) resources to see if they are NOT equal.
*
* \return Negation of == operator
*/
bool operator!=(Cuda const& c) const
{
return !(*this == c);
}

private:
cudaStream_t stream;
Expand Down
24 changes: 22 additions & 2 deletions include/camp/resource/hip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,28 @@ namespace resources
}
}

hipStream_t get_stream() { return stream; }
int get_device() { return device; }
hipStream_t get_stream() const { return stream; }
int get_device() const { return device; }

/*
* \brief Compares two (Hip) resources to see if they are equal.
*
* \return True or false depending on if this is the same stream.
*/
bool operator==(Hip const& h) const
{
return (get_stream() == h.get_stream());
}

/*
kab163 marked this conversation as resolved.
Show resolved Hide resolved
* \brief Compares two (Hip) resources to see if they are NOT equal.
*
* \return Negation of == operator
*/
bool operator!=(Hip const& h) const
{
return !(*this == h);
}

private:
hipStream_t stream;
Expand Down
21 changes: 21 additions & 0 deletions include/camp/resource/host.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,27 @@ namespace resources
void memset(void *p, int val, size_t size) { std::memset(p, val, size); }
};

/*
* \brief Compares two (Host) resources to see if they are equal.
* (This was made in to a free function to appease MSVC and SYCL compilers)
*
* \return Always return true since we are on the Host in this case.
*/
bool operator==(Host const&, Host const&)
{
return true;
}

/*
kab163 marked this conversation as resolved.
Show resolved Hide resolved
* \brief Compares two (Host) resources to see if they are NOT equal.
* (This was made in to a free function to appease MSVC and SYCL compilers)
*
* \return Always return false. Host resources are always the same.
*/
bool operator!=(Host const&, Host const&)
{
return false;
}
} // namespace v1
} // namespace resources
} // namespace camp
Expand Down
19 changes: 19 additions & 0 deletions include/camp/resource/omp_target.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,25 @@ namespace resources
}
return ret;
}
/*
* \brief Compares two (Omp) resources to see if they are equal.
*
* \return True or false depending on if this is the same dev int and addr ptr.
*/
bool operator==(Omp const& o) const
{
return (dev == o.dev && addr == o.addr);
}

/*
kab163 marked this conversation as resolved.
Show resolved Hide resolved
* \brief Compares two (Omp) resources to see if they are NOT equal.
*
* \return Negation of == operator
*/
bool operator!=(Omp const& o) const
{
return !(*this == o);
}

private:
char *addr;
Expand Down
21 changes: 21 additions & 0 deletions include/camp/resource/sycl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,27 @@ namespace resources
}

sycl::queue *get_queue() { return qu; }
kab163 marked this conversation as resolved.
Show resolved Hide resolved
sycl::queue const *get_queue() const { return qu; }

/*
* \brief Compares two (Sycl) resources to see if they are equal.
*
* \return True or false depending on if this is the same queue.
*/
bool operator==(Sycl const& s) const
{
return (get_queue() == s.get_queue());
}

/*
kab163 marked this conversation as resolved.
Show resolved Hide resolved
* \brief Compares two (Sycl) resources to see if they are NOT equal.
*
* \return Negation of == operator
*/
bool operator!=(Sycl const& s) const
{
return !(*this == s);
}

private:
sycl::queue *qu;
Expand Down
48 changes: 48 additions & 0 deletions test/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,53 @@ TEST(CampResource, GetPlatform)
ASSERT_EQ(static_cast<const Resource>(Omp()).get_platform(), Platform::omp_target);
#endif
}
TEST(CampResource, Compare)
{
Resource h1{Host()};
Resource h2{Host()};
Host h; Resource h3{h};

#ifdef CAMP_HAVE_CUDA
Resource r1{Cuda()};
Resource r2{Cuda()};
Cuda s; Resource r3{s};
#endif
#ifdef CAMP_HAVE_HIP
Resource r1{Hip()};
Resource r2{Hip()};
Hip s; Resource r3{s};
#endif
#ifdef CAMP_HAVE_OMP_OFFLOAD
Resource r1{Omp()};
Resource r2{Omp()};
Omp s; Resource r3{s};
#endif

ASSERT_TRUE(h1 == h1);
MrBurmark marked this conversation as resolved.
Show resolved Hide resolved
ASSERT_TRUE(h1 == h2);

ASSERT_FALSE(h1 != h2);

#if defined(CAMP_HAVE_CUDA) || \
defined(CAMP_HAVE_HIP) || \
defined(CAMP_HAVE_OMP_OFFLOAD)
ASSERT_TRUE(r1 == r1);
ASSERT_TRUE(r2 == r2);
ASSERT_TRUE(s == s);
ASSERT_TRUE(h == h);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be above, and there should be a != test.

ASSERT_TRUE(r1 != r2);
ASSERT_TRUE(r2 != r1);
ASSERT_TRUE(r1 != h1);
ASSERT_TRUE(r3 != h3);

ASSERT_FALSE(r1 == r2);
ASSERT_FALSE(r2 == r1);
ASSERT_FALSE(r2 == r3);
ASSERT_FALSE(h2 == r2);
ASSERT_FALSE(h3 == r3);
ASSERT_FALSE(r1 != r1);
#endif
}
TEST(CampResource, ConvertWorks)
{
Resource h1{Host()};
Expand All @@ -72,6 +119,7 @@ TEST(CampResource, Reassignment)
ASSERT_EQ(typeid(c2), typeid(h2));
}


TEST(CampResource, StreamSelect)
{
cudaStream_t stream1, stream2;
Expand Down