overrided the new [] operator

Pages: 12
new foo[] is no more suitable than std::vectoir<foo>: It will always call the constructors, sequentially.

For a quick proof-of-concept (e,g, to compare those different frameworks you listed), I would allocate raw memory (using allocator_traits<>::allocate() or std::get_temporary_buffer() or new char[])), and then construct (using allocator_traits<>::construct() or placement new) in parallel, preferably with each thread owning a contiguous cache-aligned subrange.
I agree however new foo[] can be defined for foo it might not be a good idea to define std::vector for foo

therefor the question is to create an arraylike class or to custom define new foo[]
The purpose of this exercise is to find a way to initialize 10,000+ foo objects using a parallel process such as C++ AMP, OpenMP, Threading Building Blocks, or Microsoft's Parallel Patterns Library.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <omp.h>
#include <iostream>
class foo {
   public:
      foo():a(-1) {std::cout << "foo()" << std::endl;}
      foo(int val):a(val) {std::cout << "foo(" << val << ")" << std::endl;}
      ~foo() {std::cout << "~foo(" << a << ")"<< std::endl;}
   private:
      int a;
};

int main (int argc, char *argv[]) {
  int i(0),nFoos(25),limit(20);
  foo* rawMem(reinterpret_cast<foo*>(operator new(nFoos * sizeof(foo))));

  #pragma omp parallel for if (nFoos>=limit) private(i) shared(rawMem)
  for (i=0;i<nFoos;i++) {
     new (rawMem+i) foo(i);
  }
  //  
  // Do stuff 
  // 
  #pragma omp parallel for if (nFoos>=limit) private(i) shared(rawMem)
  for (i=0;i<nFoos;i++) {
     (rawMem+i)->~foo();
  }
  // delete memory
  operator delete(rawMem);
}
$ g++ -fopenmp omp_vickoza.cpp 
$ ./a.out
foo(foo(013))

foo(foo(114))

foo(15)
foo(16)
foo(17)
foo(18)
foo(19)
foo(20)
foo(21)
foo(22)
foo(23)
foo(24)
foo(2)
foo(3)
foo(4)
foo(5)
foo(6)
foo(7)
foo(8)
foo(9)
foo(10)
foo(11)
foo(12)
~foo(13)
~foo(14)
~foo(15)
~foo(16)
~foo(17)
~foo(18)
~foo(19)
~foo(20)
~foo(21)
~foo(0)
~foo(1)
~foo(2)
~foo(3)
~foo(4)
~foo(5)
~foo(6)
~foo(7)
~foo(8)
~foo(9)
~foo(10)
~foo(11)
~foo(12)
~foo(22)
~foo(23)
~foo(24)
$
Last edited on
While I agree with this approach I would like to keep the object construction and destruction outside of the main loop. I am just debating if it is better just to override the new[] operator or create an array class similar to the C++ std::vector class without giving the ability to grow after it is created
It is not possible to do what you want to do by overriding or overloading any operator or function.

Have you heard of std::array?
http://en.cppreference.com/w/cpp/container/array
Yes notation is valid but there are two problem with using std::array one the constructor still must be overridden to call object constructors and destructors in parallel and two this is only supported in C++ 11. In this case I would like to avoid some C++ 11 constructs with the exception for implementations for some of the parallelization libraries C++ AMP, C++ threads, and Microsoft's Parallel Patterns Library. I would like to hear some other opinions before finalizing this test project
I have made a decision to create several array classes based on the std::vector and std::array classes but I would like to disable the global new [] operator
Why are you obsessed with messing wih the new[] operator?
Eventually I want to bring the code into an application. When I am using the class I want prevent from using the new[] and the delete[] operators unless I can overload constructor and destructors calling feature of new[] and the delete[] operators. Forcing myself and other who will maintain the application to use the proper classes. I am not blocking the new and delete operators for creating objects dynamically.
Thank you everyone I have created my test application using a serial array and an OpenMP array. I could not used construct C++ AMP Array due to AMP's limitation or not allowing constructors to be called in AMP. I am also having some problems with Parallel Patterns Library. Please reply if you want to know the results
Last edited on
Topic archived. No new replies allowed.
Pages: 12