This repository was archived by the owner on Mar 28, 2023. It is now read-only.
forked from llvm/llvm-test-suite
-
Notifications
You must be signed in to change notification settings - Fork 130
[SYCL] Add a test checking wrapped USM pointers #1288
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,105 @@ | ||
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out | ||
// RUN: %CPU_RUN_PLACEHOLDER %t.out | ||
// RUN: %GPU_RUN_PLACEHOLDER %t.out | ||
// RUN: %ACC_RUN_PLACEHOLDER %t.out | ||
|
||
//==---------- wrapped_usm_pointer.cpp - test pointers in struct ---------==// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//==----------------------------------------------------------------------==// | ||
|
||
#include <iostream> | ||
#include <sycl/sycl.hpp> | ||
|
||
struct Simple { | ||
int *Data; | ||
int Addition; | ||
}; | ||
|
||
struct WrapperOfSimple { | ||
int Addition; | ||
Simple Obj; | ||
}; | ||
|
||
struct NonTrivial { | ||
int Addition; | ||
int *Data; | ||
|
||
NonTrivial(int *D, int A) : Data(D), Addition(A) {} | ||
}; | ||
|
||
using namespace sycl; | ||
|
||
int main() { | ||
constexpr int NumOfElements = 7; | ||
|
||
queue Q; | ||
|
||
NonTrivial NonTrivialObj(sycl::malloc_shared<int>(NumOfElements, Q), 38); | ||
Simple SimpleObj = {sycl::malloc_shared<int>(NumOfElements, Q), 42}; | ||
WrapperOfSimple WrapperOfSimpleObj = { | ||
300, {sycl::malloc_shared<int>(NumOfElements, Q), 100500}}; | ||
|
||
// Test simple struct containing pointer. | ||
Q.parallel_for(NumOfElements, [=](id<1> Idx) { | ||
SimpleObj.Data[Idx] = Idx + SimpleObj.Addition; | ||
}); | ||
|
||
// Test simple non-trivial struct containing pointer. | ||
Q.parallel_for(NumOfElements, [=](id<1> Idx) { | ||
NonTrivialObj.Data[Idx] = Idx + NonTrivialObj.Addition; | ||
}); | ||
|
||
// Test nested struct containing pointer. | ||
Q.parallel_for(NumOfElements, [=](id<1> Idx) { | ||
WrapperOfSimpleObj.Obj.Data[Idx] = Idx + WrapperOfSimpleObj.Obj.Addition; | ||
}); | ||
|
||
// Test array of structs containing pointers. | ||
Simple SimpleArr[NumOfElements]; | ||
for (int i = 0; i < NumOfElements; ++i) { | ||
SimpleArr[i].Data = sycl::malloc_shared<int>(NumOfElements, Q); | ||
SimpleArr[i].Addition = 38 + i; | ||
} | ||
|
||
Q.parallel_for(range<2>(NumOfElements, NumOfElements), [=](item<2> Idx) { | ||
SimpleArr[Idx.get_id(0)].Data[Idx.get_id(1)] = | ||
Idx.get_id(1) + SimpleArr[Idx.get_id(0)].Addition; | ||
}); | ||
|
||
Q.wait(); | ||
|
||
auto Checker = [](auto Obj) { | ||
for (int i = 0; i < NumOfElements; ++i) { | ||
if (Obj.Data[i] != (i + Obj.Addition)) { | ||
std::cout << "line: " << __LINE__ << " result[" << i << "] is " | ||
<< Obj.Data[i] << " expected " << i + Obj.Addition | ||
<< std::endl; | ||
return true; // true if fail | ||
} | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
bool Fail = false; | ||
Fail = Checker(SimpleObj); | ||
Fail = Checker(NonTrivialObj); | ||
Fail = Checker(WrapperOfSimpleObj.Obj); | ||
|
||
for (int i = 0; i < NumOfElements; ++i) | ||
Fail = Checker(SimpleArr[i]); | ||
|
||
// Free allocated memory. | ||
sycl::free(NonTrivialObj.Data, Q); | ||
sycl::free(SimpleObj.Data, Q); | ||
sycl::free(WrapperOfSimpleObj.Obj.Data, Q); | ||
|
||
for (int i = 0; i < NumOfElements; ++i) | ||
sycl::free(SimpleArr[i].Data, Q); | ||
|
||
return Fail; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please free the allocated memory at the end of the program.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks!
Apparently simple USM examples from SYCL 2020 spec do not free memory as well :)