4.5. Save/Load a storage

We can save/read a Storage instance to/from a file.

4.5.1. Save a Storage

To save a Storage to file, simply call Storage.Save(filepath).

  • In Python:

1A = cytnx.Storage(4)
2A.fill(6)
3A.Save("S1")
  • In C++:

1auto A = cytnx::Storage(4);
2A.fill(6);
3A.Save("S1");

This will save Storage A to the current directory as T1.cyst, with extension .cyst

4.5.2. Load a Storage

Now, let’s load the Storage from the file.

  • In Python:

1A = cytnx.Storage.Load("S1.cyst")
2print(A)
  • In C++:

1auto A = cytnx::Storage::Load("S1.cyst");
2cout << A << endl;

Output >>

dtype : Double (Float64)
device: cytnx device: CPU
size  : 4
[ 6.00000e+00 6.00000e+00 6.00000e+00 6.00000e+00 ]

4.5.3. Save & load from/to binary

We can also save all the elements in a binary file without any additional header information associated to the storage format. This is similar to numpy.tofile/numpy.fromfile.

  • In Python:

 1# read
 2A = cytnx.Storage(10)
 3A.fill(10)
 4print(A)
 5
 6A.Tofile("S1")
 7
 8#load
 9B = cytnx.Storage.Fromfile("S1",cytnx.Type.Double)
10print(B)
  • In C++:

 1// read
 2auto A = cytnx::Storage(10);
 3A.fill(10);
 4cout << A << endl;
 5
 6A.Tofile("S1");
 7
 8// load
 9auto B = cytnx::Storage::Fromfile("S1", cytnx::Type.Double);
10
11cout << B << endl;

Output >>

dtype : Double (Float64)
device: cytnx device: CPU
size  : 10
[ 1.00000e+01 1.00000e+01 1.00000e+01 1.00000e+01 1.00000e+01 1.00000e+01 1.00000e+01 1.00000e+01 1.00000e+01 1.00000e+01 ]


dtype : Double (Float64)
device: cytnx device: CPU
size  : 10
[ 1.00000e+01 1.00000e+01 1.00000e+01 1.00000e+01 1.00000e+01 1.00000e+01 1.00000e+01 1.00000e+01 1.00000e+01 1.00000e+01 ]

Note

You can also choose to load a part of the file with an additional argument count when using Fromfile