项目作者: danielaparker

项目描述 :
A modern C++ header-only library for constructing N-dimensional arrays
高级语言: C++
项目地址: git://github.com/danielaparker/acons.git
创建时间: 2018-08-03T03:26:23Z
项目社区:https://github.com/danielaparker/acons

开源协议:MIT License

下载


acons

acons is a modern C++ header-only library for constructing N-dimensional arrays.

It is distributed under the MIT License.

Get acons

Download the latest single header file.

How to use it

Examples
Reference

Supported compilers

Compiler Versions Operating System
VS 14.0 Windows 10
g++- 4.8, 6, 7,8 Ubuntu
clang 3.8, 5.0. 6.0

Examples

Construct an array with zeros

  1. #include <iostream>
  2. #include <acons/ndarray.hpp>
  3. int main()
  4. {
  5. // Construct an array of shape 2 x 3 with zeros
  6. acons::ndarray<double,2> a(2,3,0.0);
  7. std::cout << a << "\n\n";
  8. }

Output:

  1. [[0,0,0],[0,0,0]]

Construct a 3-dim array

  1. #include <iostream>
  2. #include <acons/ndarray.hpp>
  3. int main()
  4. {
  5. // Create a 3-dim array of shape 3 x 4 x 2
  6. acons::ndarray<double,3> a(3,4,2);
  7. // Assign values to the elements
  8. int x = 0;
  9. for (size_t i = 0; i < a.shape(0); ++i)
  10. {
  11. for (size_t j = 0; j < a.shape(1); ++j)
  12. {
  13. for (size_t k = 0; k < a.shape(2); ++k)
  14. {
  15. a(i,j,k) = x++;
  16. }
  17. }
  18. }
  19. // Print
  20. std::cout << a << "\n";
  21. }

Output:

  1. [[[0,1],[2,3],[4,5],[6,7]],[[8,9],[10,11],[12,13],[14,15]],[[16,17],[18,19],[20,21],[22,23]]]

Wrap a C-array

  1. #include <iostream>
  2. #include <acons/ndarray.hpp>
  3. int main()
  4. {
  5. double a[] = {0,1,2,3,4,5,6};
  6. // Elements of a can be modified through this 2 x 3 interface
  7. acons::ndarray_view<double,2> v(a,2,3);
  8. v(0,2) = 9;
  9. // Elements of a cannot be modified through this 2 x 3 interface
  10. acons::const_ndarray_view<double,2> cv(a,2,3);
  11. std::cout << cv << "\n\n";
  12. }

Output:

  1. [[0,1,9],[3,4,5]]

Slicing a 1-dimensional array

A slice object can be constructed with start, stop and step parameters.

  1. #include <iostream>
  2. #include <acons/ndarray.hpp>
  3. int main()
  4. {
  5. acons::ndarray<double,1> a = {0,1,2,3,4,5,6,7,8,9};
  6. // Extracting a part of the array with a slice object
  7. acons::ndarray_view<double,1> v(a, {acons::slice(2,7,2)});
  8. std::cout << v << "\n\n";
  9. }

Output:

  1. [2,4,6]

Slicing a 2-dimensional array

A slice object constructed with no parameters identifies all the elements along the dimension of an array.
If stop and step are not given, stop defaults to one past the last element along the dimension,
and step defaults to 1.

  1. #include <iostream>
  2. #include <acons/ndarray.hpp>
  3. int main()
  4. {
  5. // Construct a 2-dimensional 3 x 4 array
  6. acons::ndarray<double,2> a = {{0,1,2,3},{4,5,6,7},{8,9,10,11}};
  7. // All items from row 1 and columns 0 and 1
  8. acons::ndarray_view<double,2> v1(a, {acons::slice(1,2),acons::slice(0,2)});
  9. std::cout << "(1) " << v1 << "\n\n";
  10. // All items from column 1 onwards
  11. acons::ndarray_view<double,2> v2(a, {acons::slice(),acons::slice(1)});
  12. std::cout << "(2) " << v2 << "\n\n";
  13. }

Output:

  1. (1) [[4,5]]
  2. (2) [[1,2,3],[5,6,7],[9,10,11]]

Reduction

Construct a view on an array (or another view) that has fewer dimensions than the original.

  1. #include <iostream>
  2. #include <acons/ndarray.hpp>
  3. int main()
  4. {
  5. // Construct a 2-dimensional 3 x 4 array
  6. acons::ndarray<double,2> a = {{0,1,2,3},{4,5,6,7},{8,9,10,11}};
  7. // Reduce the 2-dimensional array to a 1-dimensional array, along row 0
  8. acons::ndarray_view<double,1> v1(a, acons::indices_t<1>{0});
  9. std::cout << "(1) " << v1 << "\n\n";
  10. // Reduce the 2-dimensional array to a 1-dimensional array, along column 2
  11. acons::ndarray_view<double,1> v2(a, {acons::slice()}, acons::indices_t<1>{2});
  12. std::cout << "(2) " << v2 << "\n\n";
  13. }

Output:

  1. (1) [[12,13,14,15],[16,17,18,19],[20,21,22,23]]
  2. (2) [14,18,22]

Creating ndarrays in managed shared memory with Boost interprocess allocators

  1. #include <boost/interprocess/managed_shared_memory.hpp>
  2. #include <boost/interprocess/containers/vector.hpp>
  3. #include <boost/interprocess/allocators/allocator.hpp>
  4. #include <boost/interprocess/containers/string.hpp>
  5. #include <cstdlib> //std::system
  6. #include <acons/ndarray.hpp>
  7. #include <iostream>
  8. typedef boost::interprocess::allocator<double,
  9. boost::interprocess::managed_shared_memory::segment_manager> shmem_allocator;
  10. typedef acons::ndarray<double,2,acons::row_major,zero_based,shmem_allocator> ndarray_shm;
  11. int main(int argc, char *argv[])
  12. {
  13. typedef std::pair<double, int> MyType;
  14. if(argc == 1) //Parent process
  15. {
  16. //Remove shared memory on construction and destruction
  17. struct shm_remove
  18. {
  19. shm_remove() { boost::interprocess::shared_memory_object::remove("MySharedMemory"); }
  20. ~shm_remove(){ boost::interprocess::shared_memory_object::remove("MySharedMemory"); }
  21. }
  22. remover;
  23. //Construct managed shared memory
  24. boost::interprocess::managed_shared_memory segment(boost::interprocess::create_only,
  25. "MySharedMemory", 65536);
  26. //Initialize shared memory STL-compatible allocator
  27. const shmem_allocator allocator(segment.get_segment_manager());
  28. // Create ndarray with all dynamic allocations in shared memory
  29. ndarray_shm* pA = segment.construct<ndarray_shm>("my ndarray")(std::allocator_arg, allocator, 2, 2, 0.0);
  30. ndarray_shm& a = *pA;
  31. a(0,0) = 0;
  32. a(0,1) = 1;
  33. a(1,0) = 2;
  34. a(1,1) = 3;
  35. std::pair<ndarray_shm*, boost::interprocess::managed_shared_memory::size_type> res;
  36. res = segment.find<ndarray_shm>("my ndarray");
  37. std::cout << "Parent process:\n";
  38. std::cout << *(res.first) << "\n";
  39. //Launch child process
  40. std::string s(argv[0]); s += " child ";
  41. if(0 != std::system(s.c_str()))
  42. return 1;
  43. //Check child has destroyed all objects
  44. if(segment.find<MyType>("my ndarray").first)
  45. return 1;
  46. }
  47. else
  48. {
  49. //Open managed shared memory
  50. boost::interprocess::managed_shared_memory segment(boost::interprocess::open_only,
  51. "MySharedMemory");
  52. std::pair<ndarray_shm*, boost::interprocess::managed_shared_memory::size_type> res;
  53. res = segment.find<ndarray_shm>("my ndarray");
  54. if (res.first != nullptr)
  55. {
  56. std::cout << "\nChild process:\n";
  57. std::cout << *(res.first) << "\n";
  58. }
  59. else
  60. {
  61. std::cout << "Result is null\n";
  62. }
  63. //We're done, delete all the objects
  64. segment.destroy<ndarray_shm>("my ndarray");
  65. }
  66. }

Output:

  1. Parent process:
  2. [[0,1],[2,3]]
  3. Child process:
  4. [[0,1],[2,3]]