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

Use std::aligned_alloc for allocations #6341

Merged
merged 7 commits into from
Aug 11, 2023

Conversation

masterleinad
Copy link
Contributor

@masterleinad masterleinad commented Aug 7, 2023

Related to #5011. It seems that we don't need all these different allocation mechanisms anymore and can use std::aligned_alloc(https://en.cppreference.com/w/cpp/memory/c/aligned_alloc) instead. Also, there were some problems with INTEL_MM_ALLOC reported that don't appear anymore when using std::malloc instead.
Note that MSVC is unable to handle aligned allocations of any kind and provides _aligned_malloc (to be freed with _aligned_free) instead.

This pull request deprecates all other allocation variants for HostSpace to only use aligned_malloc.

HBWSpace is untested and broken, see #4889. I would address it in a different pull request (maybe that one).

@masterleinad masterleinad force-pushed the use_aligned_alloc branch 3 times, most recently from b507200 to 5854e75 Compare August 7, 2023 22:59
@masterleinad masterleinad marked this pull request as ready for review August 8, 2023 11:35
Copy link
Contributor

@cz4rs cz4rs left a comment

Choose a reason for hiding this comment

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

Looks good to me 👍

@masterleinad
Copy link
Contributor Author

masterleinad commented Aug 8, 2023

https://github.com/zippylab should be able to comment some more on the issue found with using _mm_malloc.

@zippylab
Copy link

zippylab commented Aug 8, 2023

https://github.com/zippylab should be able to comment some more on the issue found with using _mm_malloc.

We had been seeing crashes of the XGC application on the Sunspot system (Aurora test system) coming from this line in HostSpace.cpp (in Kokkos::HostSpace::impl_deallocate):

(gdb) list
212           void *alloc_ptr = *(reinterpret_cast<void **>(arg_alloc_ptr) - 1);
/213           free(alloc_ptr);
214         }
215     #if defined(KOKKOS_ENABLE_INTEL_MM_ALLOC)
216         else if (m_alloc_mech == INTEL_MM_ALLOC) {
217           _mm_free(arg_alloc_ptr);  <<<<<<<<<<<<<<<<<<HERE<<<<<<
218         }
219     #endif
220       }
221     }
(gdb)

in a part of XGC that is copying particle data from device to host memory (using Cabana::SoA and Cabana::AoSoA).

After rebuilding Kokkos, Cabana, and XGC with the changes in this PR, that failure mode of XGC went away, and it ran past the point where the SEGV was happening before in the same test case.

Copy link
Member

@dalg24 dalg24 left a comment

Choose a reason for hiding this comment

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

What about RawMemoryAllocationFailure::AllocationMechanism?
Should we not deprecate enumerators that are not supported?

core/src/impl/Kokkos_HostSpace.cpp Outdated Show resolved Hide resolved
size_t size_alignment_multiple =
(arg_alloc_size + alignment - 1) / alignment * alignment;
#ifdef KOKKOS_COMPILER_MSVC
ptr = _aligned_malloc(size_alignment_multiple, alignment);
Copy link
Member

Choose a reason for hiding this comment

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

Does _aligned_malloc also require the size argument to be a multiple of the alignment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I couldn't find information about that but preferred doing it anyway for consistency.

Copy link
Member

Choose a reason for hiding this comment

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

Does MSVC not support aligned_alloc?

Copy link
Contributor

Choose a reason for hiding this comment

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

MSVC indeed does not support aligned_alloc as noted on CppReference.com

It would be nice to be able to use operator new(size_alignment_multiple, std::align_val_t(alignment)) instead, avoiding the conditional, but then we'd have to store the alignment to pass to the corresponding operator delete

core/src/OpenMP/Kokkos_OpenMP_Instance.cpp Show resolved Hide resolved
core/src/Kokkos_HostSpace.hpp Show resolved Hide resolved
@masterleinad
Copy link
Contributor Author

masterleinad commented Aug 9, 2023

What about RawMemoryAllocationFailure::AllocationMechanism?
Should we not deprecate enumerators that are not supported?

Sure, see 8c8c9c3.

Copy link
Member

@crtrott crtrott left a comment

Choose a reason for hiding this comment

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

I don't think the print stuff works?

size_t size_alignment_multiple =
(arg_alloc_size + alignment - 1) / alignment * alignment;
#ifdef KOKKOS_COMPILER_MSVC
ptr = _aligned_malloc(size_alignment_multiple, alignment);
Copy link
Member

Choose a reason for hiding this comment

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

Does MSVC not support aligned_alloc?

@@ -102,11 +102,13 @@ void Experimental::RawMemoryAllocationFailure::print_error_message(
o << " (The allocation mechanism was ";
switch (m_mechanism) {
case AllocationMechanism::StdMalloc: o << "standard malloc()."; break;
Copy link
Member

Choose a reason for hiding this comment

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

How does this work if KOKKOS_ENABLE_DEPRECATED_CODE_4 is off? Isn't the entire AllocationMechanism thing removed if DEPRECATED_CODE_4 is not enabled?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are looking at the wrong place, the relevant is core/src/impl/Kokkos_Error.hpp, not core/src/Kokkos_HostSpace.hpp. Both files contain AllocationMechanism enums but they are different. Note that we test with KOKKOS_DEPRECATED_CODE_4=OFF in our CI, see

kokkos/.jenkins

Lines 56 to 92 in 1a3ea28

stage('CUDA-11.7-NVHPC') {
agent {
dockerfile {
filename 'Dockerfile.nvhpc'
dir 'scripts/docker'
additionalBuildArgs '--build-arg BASE=nvcr.io/nvidia/nvhpc:22.9-devel-cuda11.7-ubuntu20.04'
label 'nvidia-docker && large_images'
args '-v /tmp/ccache.kokkos:/tmp/ccache --env NVIDIA_VISIBLE_DEVICES=$NVIDIA_VISIBLE_DEVICES'
}
}
environment {
OMP_NUM_THREADS = 8
// Nested OpenMP does not work for this configuration,
// so disabling it
OMP_MAX_ACTIVE_LEVELS = 1
OMP_PLACES = 'threads'
OMP_PROC_BIND = 'spread'
NVHPC_CUDA_HOME = '/opt/nvidia/hpc_sdk/Linux_x86_64/22.9/cuda/11.7'
}
steps {
sh '''rm -rf build && mkdir -p build && cd build && \
/opt/cmake/bin/cmake \
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DCMAKE_CXX_COMPILER=nvc++ \
-DCMAKE_CXX_STANDARD=17 \
-DCMAKE_CXX_FLAGS="--diag_suppress=implicit_return_from_non_void_function,no_device_stack" \
-DKokkos_ARCH_NATIVE=ON \
-DKokkos_ENABLE_COMPILER_WARNINGS=ON \
-DKokkos_ENABLE_DEPRECATED_CODE_4=OFF \
-DKokkos_ENABLE_TESTS=ON \
-DKokkos_ENABLE_CUDA=ON \
-DKokkos_ENABLE_CUDA_LAMBDA=ON \
-DKokkos_ENABLE_OPENMP=ON \
.. && \
make -j8 && ctest --verbose'''
}
}
for example.

Copy link
Member

Choose a reason for hiding this comment

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

Ok that seems somewhat unfortunate naming but ok.

@masterleinad
Copy link
Contributor Author

Note that we get a couple of warnings (if we enable KOKKOS_DEPRECATED_CODE_4 and deprecation warnings; here with warnings as errors)

/tmp/kokkos/core/src/impl/Kokkos_Error.cpp: In member function 'void Kokkos::Experimental::RawMemoryAllocationFailure::print_error_message(std::ostream&) const':
/tmp/kokkos/core/src/impl/Kokkos_Error.cpp:106:31: error: 'Kokkos::Experimental::RawMemoryAllocationFailure::AllocationMechanism::PosixMemAlign' is deprecated [-Werror=deprecated-declarations]
  106 |     case AllocationMechanism::PosixMemAlign: o << "posix_memalign()."; break;
      |                               ^~~~~~~~~~~~~
In file included from /tmp/kokkos/core/src/impl/Kokkos_Error.cpp:28:
/tmp/kokkos/core/src/impl/Kokkos_Error.hpp:117:5: note: declared here
  117 |     PosixMemAlign KOKKOS_DEPRECATED,
      |     ^~~~~~~~~~~~~
/tmp/kokkos/core/src/impl/Kokkos_Error.cpp:106:31: error: 'Kokkos::Experimental::RawMemoryAllocationFailure::AllocationMechanism::PosixMemAlign' is deprecated [-Werror=deprecated-declarations]
  106 |     case AllocationMechanism::PosixMemAlign: o << "posix_memalign()."; break;
      |                               ^~~~~~~~~~~~~
/tmp/kokkos/core/src/impl/Kokkos_Error.hpp:117:5: note: declared here
  117 |     PosixMemAlign KOKKOS_DEPRECATED,
      |     ^~~~~~~~~~~~~
/tmp/kokkos/core/src/impl/Kokkos_Error.cpp:107:31: error: 'Kokkos::Experimental::RawMemoryAllocationFailure::AllocationMechanism::PosixMMap' is deprecated [-Werror=deprecated-declarations]
  107 |     case AllocationMechanism::PosixMMap: o << "POSIX mmap()."; break;
      |                               ^~~~~~~~~
/tmp/kokkos/core/src/impl/Kokkos_Error.hpp:118:5: note: declared here
  118 |     PosixMMap KOKKOS_DEPRECATED,
      |     ^~~~~~~~~
/tmp/kokkos/core/src/impl/Kokkos_Error.cpp:107:31: error: 'Kokkos::Experimental::RawMemoryAllocationFailure::AllocationMechanism::PosixMMap' is deprecated [-Werror=deprecated-declarations]
  107 |     case AllocationMechanism::PosixMMap: o << "POSIX mmap()."; break;
      |                               ^~~~~~~~~
/tmp/kokkos/core/src/impl/Kokkos_Error.hpp:118:5: note: declared here
  118 |     PosixMMap KOKKOS_DEPRECATED,
      |     ^~~~~~~~~
/tmp/kokkos/core/src/impl/Kokkos_Error.cpp:108:31: error: 'Kokkos::Experimental::RawMemoryAllocationFailure::AllocationMechanism::IntelMMAlloc' is deprecated [-Werror=deprecated-declarations]
  108 |     case AllocationMechanism::IntelMMAlloc:
      |                               ^~~~~~~~~~~~
/tmp/kokkos/core/src/impl/Kokkos_Error.hpp:119:5: note: declared here
  119 |     IntelMMAlloc KOKKOS_DEPRECATED,
      |     ^~~~~~~~~~~~
/tmp/kokkos/core/src/impl/Kokkos_Error.cpp:108:31: error: 'Kokkos::Experimental::RawMemoryAllocationFailure::AllocationMechanism::IntelMMAlloc' is deprecated [-Werror=deprecated-declarations]
  108 |     case AllocationMechanism::IntelMMAlloc:
      |                               ^~~~~~~~~~~~
/tmp/kokkos/core/src/impl/Kokkos_Error.hpp:119:5: note: declared here
  119 |     IntelMMAlloc KOKKOS_DEPRECATED,
      |     ^~~~~~~~~~~~
cc1plus: all warnings being treated as errors
make[2]: *** [core/src/CMakeFiles/kokkoscore.dir/impl/Kokkos_Error.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....
/tmp/kokkos/core/src/impl/Kokkos_HostSpace.cpp:59:60: error: 'AllocationMechanism' is deprecated [-Werror=deprecated-declarations]
   59 | HostSpace::HostSpace(const HostSpace::AllocationMechanism &) : HostSpace() {}
      |                                                            ^
In file included from /tmp/kokkos/core/src/impl/Kokkos_HostSpace.cpp:49:
/tmp/kokkos/core/src/Kokkos_HostSpace.hpp:78:26: note: declared here
   78 |   enum KOKKOS_DEPRECATED AllocationMechanism {
      |                          ^~~~~~~~~~~~~~~~~~~

but they all originate from "*.cc" files.

Copy link
Member

@dalg24 dalg24 left a comment

Choose a reason for hiding this comment

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

What about HBWSpace?

core/src/impl/Kokkos_HostSpace.cpp Outdated Show resolved Hide resolved
core/src/impl/Kokkos_Error.cpp Outdated Show resolved Hide resolved
core/src/OpenMP/Kokkos_OpenMP_Instance.cpp Show resolved Hide resolved
core/src/impl/Kokkos_HostSpace.cpp Show resolved Hide resolved
@masterleinad
Copy link
Contributor Author

What about HBWSpace?

HBWSpace is untested and broken, see #4889. I would address it in a different pull request (maybe that one).

@dalg24
Copy link
Member

dalg24 commented Aug 10, 2023

What about HBWSpace?

HBWSpace is untested and broken, see #4889. I would address it in a different pull request (maybe that one).

Add a note in the PR description that you intentionally did not update HBWSpace.

@masterleinad
Copy link
Contributor Author

Add a note in the PR description that you intentionally did not update HBWSpace.

Here you go!

@masterleinad masterleinad mentioned this pull request Aug 10, 2023
@dalg24 dalg24 merged commit 8d8b24a into kokkos:develop Aug 11, 2023
25 of 28 checks passed
@barracuda156
Copy link
Contributor

@masterleinad @dalg24 @crtrott Unfortunately, this does not work for all platforms: #6367

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants