-
Notifications
You must be signed in to change notification settings - Fork 20
This adds a file containing Adapt functions for a number of structs. This allows Adapt.adapt to be called with structs to change the underlying data type. #195
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
Open
Nielsbk
wants to merge
4
commits into
PartitionedArrays:master
Choose a base branch
from
Nielsbk:adapt
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 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 |
---|---|---|
|
@@ -4,6 +4,7 @@ authors = ["Francesc Verdugo <[email protected]> and contributors"] | |
version = "0.5.10" | ||
|
||
[deps] | ||
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" | ||
BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e" | ||
CircularArrays = "7a955b69-7140-5f4e-a0ed-f168c5e2e749" | ||
Distances = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7" | ||
|
@@ -18,6 +19,7 @@ SparseMatricesCSR = "a0a7dd2c-ebf4-11e9-1f05-cf50bc540ca1" | |
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" | ||
|
||
[compat] | ||
Adapt = "4.3.0" | ||
BlockArrays = "0.16, 1" | ||
CircularArrays = "1" | ||
Distances = "0.10" | ||
|
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
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,52 @@ | ||
|
||
function Adapt.adapt_structure(to,v::DebugArray) | ||
v = map(v) do val | ||
Adapt.adapt_structure(to,val) | ||
end | ||
end | ||
|
||
function Adapt.adapt_structure(to,v::MPIArray) | ||
v = map(v) do val | ||
Adapt.adapt_structure(to,val) | ||
end | ||
end | ||
|
||
function Adapt.adapt_structure(to,v::SplitMatrixBlocks) | ||
own_own = Adapt.adapt(to,v.own_own) | ||
own_ghost = Adapt.adapt(to,v.own_ghost) | ||
ghost_ghost = Adapt.adapt(to,v.ghost_ghost) | ||
ghost_own = Adapt.adapt(to,v.ghost_own) | ||
split_matrix_blocks(own_own,own_ghost,ghost_own,ghost_ghost) | ||
end | ||
|
||
function Adapt.adapt_structure(to,v::SplitVectorBlocks) | ||
own = Adapt.adapt(to,v.own) | ||
ghost = Adapt.adapt(to,v.ghost) | ||
split_vector_blocks(own,ghost) | ||
end | ||
|
||
function Adapt.adapt_structure(to,v::SplitVector) | ||
blocks = Adapt.adapt(to,v.blocks) | ||
perm = Adapt.adapt(to,v.permutation) | ||
split_vector(blocks,perm) | ||
end | ||
|
||
function Adapt.adapt_structure(to,v::JaggedArray) | ||
data = Adapt.adapt_structure(to,v.data) | ||
ptrs = Adapt.adapt_structure(to,v.ptrs) | ||
jagged_array(data, ptrs) | ||
end | ||
|
||
function Adapt.adapt_structure(to,v::SplitMatrix) | ||
blocks = Adapt.adapt_structure(to,v.blocks) | ||
col_per = v.col_permutation | ||
row_per = v.row_permutation | ||
split_matrix(blocks,row_per,col_per) | ||
end | ||
|
||
function Adapt.adapt_structure(to,v::PSparseMatrix) | ||
matrix_partition = Adapt.adapt_structure(to,v.matrix_partition) | ||
col_par = v.col_partition | ||
row_par = v.row_partition | ||
PSparseMatrix(matrix_partition,row_par,col_par,v.assembled) | ||
end | ||
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,64 @@ | ||
using Test | ||
using PartitionedArrays | ||
using Adapt | ||
|
||
struct FakeCuVector{A} <: AbstractVector{Float64} | ||
vector::A | ||
end | ||
|
||
Base.size(v::FakeCuVector) = size(v.vector) | ||
Base.getindex(v::FakeCuVector,i::Integer) = v.vector[i] | ||
|
||
function Adapt.adapt_storage(::Type{<:FakeCuVector},x::AbstractArray) | ||
FakeCuVector(x) | ||
end | ||
|
||
function adapt_tests(distribute) | ||
|
||
rank = distribute(LinearIndices((2,2))) | ||
|
||
a = [[1,2],[3,4,5],Int[],[3,4]] | ||
b = JaggedArray(a) | ||
c = deepcopy(b) | ||
|
||
c = Adapt.adapt(FakeCuVector,c) | ||
|
||
@test typeof(c.data) == FakeCuVector{typeof(b.data)} | ||
@test typeof(c.ptrs) == FakeCuVector{typeof(b.ptrs)} | ||
@test typeof(c).name.wrapper == GenericJaggedArray | ||
|
||
a = [1,2,3,4,5] | ||
b = deepcopy(a) | ||
b = Adapt.adapt(FakeCuVector,b) | ||
@test typeof(b) == FakeCuVector{typeof(a)} | ||
@test b.vector == a | ||
|
||
own = [1,2,3,4] | ||
ghost = [5,6,7,8] | ||
block_a = split_vector_blocks(own, ghost) | ||
block_b = deepcopy(block_a) | ||
block_b = Adapt.adapt(FakeCuVector,block_b) | ||
@test block_b.own.vector == block_a.own | ||
@test block_b.ghost.vector == block_a.ghost | ||
@test typeof(block_b.own) == FakeCuVector{typeof(block_a.own)} | ||
@test typeof(block_b.ghost) == FakeCuVector{typeof(block_a.ghost)} | ||
|
||
|
||
a = split_vector(block_a,[1,2,3,4,5,6,7,8]) | ||
b = deepcopy(a) | ||
b = Adapt.adapt(FakeCuVector,b) | ||
|
||
@test b.blocks.own.vector == a.blocks.own | ||
@test b.blocks.ghost.vector == a.blocks.ghost | ||
@test b.permutation.vector == a.permutation | ||
|
||
|
||
a = distribute([[1,1,1],[2,2,2],[3,3,3],[4,4,4]]) | ||
b = distribute([[1,1,1],[2,2,2],[3,3,3],[4,4,4]]) | ||
b = Adapt.adapt(FakeCuVector,b) | ||
|
||
map(a,b) do val_a,val_b | ||
@test typeof(val_b) == FakeCuVector{typeof(val_a)} | ||
@test val_b.vector == val_a | ||
end | ||
end |
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,9 @@ | ||
module DebugArrayAdaptTests | ||
|
||
using PartitionedArrays | ||
|
||
include(joinpath("..","adapt_tests.jl")) | ||
|
||
with_debug(adapt_tests) | ||
|
||
end # module |
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
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,5 @@ | ||
using MPI | ||
include("run_mpi_driver.jl") | ||
file = joinpath(@__DIR__,"drivers","adapt_tests.jl") | ||
run_mpi_driver(file;procs=4) | ||
|
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,9 @@ | ||
module MPIArrayAdaptTests | ||
|
||
using PartitionedArrays | ||
|
||
include(joinpath("..","..","adapt_tests.jl")) | ||
|
||
with_mpi(adapt_tests) | ||
|
||
end # module |
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
Oops, something went wrong.
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.
I think we can just use
Adapt.@adapt_structure T
for these and the generic counterparts.