3
0
mirror of https://github.com/triqs/dft_tools synced 2024-12-27 06:43:40 +01:00
dft_tools/test/triqs/arrays/views.cpp
Olivier Parcollet fc2a620eae arrays: new ref counting and weak views
- improve the mem_block and shared_block.
- the reference counting is now done in the mem_block and shared_block, removing the need of shared_ptr.

- speed tests shows that shared_ptr is very slow (due to thread safety?)
the new version is much better, though not perfect.

- Hence introducing weak views.

- also :
-- clean the guard mechanism for python (to allow returning from python without any python ref left).
-- clean code, add documentation for mem_block
-- remove nan init, which was not working, and corresponding test
-- serialisation of view still unchanged (need to forbid serialization of view ??).

- tests ok, incl. valgrind tests.
2013-07-23 08:13:44 +02:00

80 lines
2.2 KiB
C++

/*******************************************************************************
*
* TRIQS: a Toolbox for Research in Interacting Quantum Systems
*
* Copyright (C) 2011 by O. Parcollet
*
* TRIQS is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* TRIQS is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* TRIQS. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
#include "./common.hpp"
#include "./src/array.hpp"
#include <iostream>
using std::cout; using std::endl;
using namespace triqs::arrays;
int main(int argc, char **argv) {
{
std::cout<<"test doc eg"<<std::endl;
array<int,2> *p = new array<int,2> (2,3); // create an array p
for (int i =0; i<2; ++i)
for (int j=0; j<3; ++j)
(*p)(i,j) = 10*i+ j;
array_view<int,2> B(*p); // making a view
delete p; // p is gone...
B(0,0) = 314;
std::cout<<B<<std::endl<<"Done"<<std::endl; // ok, but B (and the data) is still alive
}
array<long,2> A (2,3);
for (int i =0; i<2; ++i)
for (int j=0; j<3; ++j)
A(i,j) = 10*i+ j;
array_view<long,2 > AA (A);
std::cout<<"A is "<<A<<std::endl;
std::cout<<"another view : "<<AA<<std::endl;
{
array_view<long,1> SL( A(0,range(0,3)));
array_view<long,1> SL2( A(1,range(0,2)));
array_view<long,1> SL2b( A(1,range(1,3)));
std::cout<<"SLICE : A(0,range(0,3)) "<<SL<<std::endl;
std::cout<<"SLICE : A(1,range(0,2)) "<<SL2<<std::endl;
std::cout<<"SLICE : A(1,range(1,3)) "<<SL2b<<std::endl;
}
{
array_view<long,1> SL( A(range(0,2),0));
array_view<long,1> SL2( A(range(0,2),1));
std::cout<<"SLICE : A(range(0,2),0)) "<<SL<<std::endl;
std::cout<<"SLICE : A(range(0,2),1)) "<<SL2<<std::endl;
}
array_view<long,2> V(A);
std::cout<< V(0,0)<<std::endl;
std::cout<< V<<std::endl;
return 0;
}