MCPcopy Create free account
hub / github.com/arrayfire/arrayfire / save

Function save

src/api/c/stream.cpp:41–126  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

39
40template<typename T>
41static int save(const char *key, const af_array arr, const char *filename,
42 const bool append = false) {
43 // (char ) Version (Once)
44 // (int ) No. of Arrays (Once)
45 // (int ) Length of the key
46 // (cstring) Key
47 // (intl ) Offset bytes to next array (type + dims + data)
48 // (char ) Type
49 // (intl ) dim4 (x 4)
50 // (T ) data (x elements)
51 // Setup all the data structures that need to be written to file
52 ///////////////////////////////////////////////////////////////////////////
53 std::string k(key);
54 int klen = k.size();
55
56 const ArrayInfo &info = getInfo(arr);
57 std::vector<T> data(info.elements());
58
59 AF_CHECK(af_get_data_ptr(&data.front(), arr));
60
61 char type = info.getType();
62
63 intl odims[4];
64 for (int i = 0; i < 4; i++) { odims[i] = info.dims()[i]; }
65
66 intl offset = sizeof(char) + 4 * sizeof(intl) + info.elements() * sizeof(T);
67 ///////////////////////////////////////////////////////////////////////////
68
69 std::fstream fs;
70 int n_arrays = 0;
71
72 if (append) {
73 std::ifstream checkIfExists(filename);
74 bool exists = checkIfExists.good();
75 checkIfExists.close();
76 if (exists) {
77 fs.open(filename, std::fstream::in | std::fstream::out |
78 std::fstream::binary);
79 } else {
80 fs.open(filename, std::fstream::out | std::fstream::binary);
81 }
82
83 // Throw exception if file is not open
84 if (!fs.is_open()) { AF_ERROR("File failed to open", AF_ERR_ARG); }
85
86 // Assert Version
87 if (fs.peek() == std::fstream::traits_type::eof()) {
88 // File is empty
89 fs.clear();
90 } else {
91 char prev_version = 0;
92 fs.read(&prev_version, sizeof(char));
93
94 AF_ASSERT(
95 prev_version == sfv_char,
96 "ArrayFire data format has changed. Can't append to file");
97
98 fs.read(reinterpret_cast<char *>(&n_arrays), sizeof(int));

Callers

nothing calls this directly

Calls 6

closeMethod · 0.80
writeMethod · 0.80
af_get_data_ptrFunction · 0.70
elementsMethod · 0.45
getTypeMethod · 0.45
dimsMethod · 0.45

Tested by

no test coverage detected