2021-04-29 23:41:23 +02:00
|
|
|
// This files is almost the same as test_h5.cpp, with the difference that it
|
|
|
|
// dumps Verificarlo probes for vfc_ci integration, and that it reads a list of
|
|
|
|
// cycles in a CSV file, instead of accepting a start and an end cycle (which
|
|
|
|
// makes it easier to select the exact cycles we are interested in with vfc_ci).
|
|
|
|
|
|
|
|
#include <hdf5/serial/H5Cpp.h>
|
2021-05-10 14:50:45 +02:00
|
|
|
#include <hdf5/serial/hdf5.h>
|
2021-04-29 23:41:23 +02:00
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
2021-05-10 14:50:45 +02:00
|
|
|
#include <vector>
|
2021-04-29 23:41:23 +02:00
|
|
|
|
2021-06-04 16:50:49 +02:00
|
|
|
#include "Helpers.hpp"
|
2021-05-07 17:11:04 +02:00
|
|
|
#include "SM_Maponi.hpp"
|
2021-04-29 23:41:23 +02:00
|
|
|
#include "SM_Standard.hpp"
|
|
|
|
#include "vfc_probe.h"
|
|
|
|
|
|
|
|
using namespace H5;
|
|
|
|
// #define DEBUG
|
|
|
|
|
2021-05-10 14:50:45 +02:00
|
|
|
const H5std_string FILE_NAME("datasets/ci_dataset.hdf5");
|
2021-04-29 23:41:23 +02:00
|
|
|
|
2021-05-10 14:50:45 +02:00
|
|
|
void read_int(H5File file, std::string key, unsigned int *data) {
|
2021-04-29 23:41:23 +02:00
|
|
|
DataSet ds = file.openDataSet(key);
|
|
|
|
ds.read(data, PredType::STD_U32LE);
|
|
|
|
ds.close();
|
|
|
|
}
|
|
|
|
|
2021-05-10 14:50:45 +02:00
|
|
|
void read_double(H5File file, std::string key, double *data) {
|
2021-04-29 23:41:23 +02:00
|
|
|
DataSet ds = file.openDataSet(key);
|
|
|
|
ds.read(data, PredType::IEEE_F64LE);
|
|
|
|
ds.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return a vector containing all cycles to execute by reading a data file */
|
|
|
|
std::vector<int> get_cycles_list(std::string path) {
|
|
|
|
std::ifstream file_stream(path);
|
|
|
|
std::stringstream string_stream;
|
|
|
|
string_stream << file_stream.rdbuf();
|
|
|
|
|
|
|
|
std::string cycle_str;
|
|
|
|
std::vector<int> cycles_list = {};
|
|
|
|
|
2021-05-10 14:50:45 +02:00
|
|
|
while (string_stream >> cycle_str) {
|
2021-04-29 23:41:23 +02:00
|
|
|
cycles_list.push_back(std::stoi(cycle_str));
|
|
|
|
}
|
|
|
|
|
|
|
|
return cycles_list;
|
|
|
|
}
|
|
|
|
|
2021-05-10 14:50:45 +02:00
|
|
|
int test_cycle(H5File file, int cycle, std::string version,
|
|
|
|
vfc_probes *probes) {
|
2021-04-29 23:41:23 +02:00
|
|
|
|
|
|
|
/* Read the data */
|
|
|
|
|
|
|
|
std::string group = "cycle_" + std::to_string(cycle);
|
|
|
|
|
2021-05-03 13:59:43 +02:00
|
|
|
// This will result in the same string as group but with the cycle number
|
|
|
|
// being zero-padded. This is used when calling vfc_put_probe later on.
|
|
|
|
std::string zero_padded_group = std::to_string(cycle);
|
|
|
|
zero_padded_group = "cycle_" +
|
2021-05-10 14:50:45 +02:00
|
|
|
std::string(5 - zero_padded_group.length(), '0') +
|
|
|
|
zero_padded_group;
|
2021-05-03 13:59:43 +02:00
|
|
|
|
2021-05-10 14:50:45 +02:00
|
|
|
try {
|
2021-04-29 23:41:23 +02:00
|
|
|
file.openGroup(group);
|
2021-05-10 14:50:45 +02:00
|
|
|
} catch (H5::Exception &e) {
|
2021-04-29 23:41:23 +02:00
|
|
|
std::cerr << "group " << group << "not found" << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int dim, nupdates, col, i, j;
|
|
|
|
read_int(file, group + "/slater_matrix_dim", &dim);
|
|
|
|
read_int(file, group + "/nupdates", &nupdates);
|
|
|
|
|
2021-05-10 14:50:45 +02:00
|
|
|
double *slater_matrix = new double[dim * dim];
|
2021-04-29 23:41:23 +02:00
|
|
|
read_double(file, group + "/slater_matrix", slater_matrix);
|
|
|
|
|
2021-05-10 14:50:45 +02:00
|
|
|
double *slater_inverse = new double[dim * dim];
|
2021-04-29 23:41:23 +02:00
|
|
|
read_double(file, group + "/slater_inverse", slater_inverse);
|
2021-05-10 14:50:45 +02:00
|
|
|
// slater_inverse = transpose(slater_inverse, dim);
|
2021-04-29 23:41:23 +02:00
|
|
|
|
2021-05-10 14:50:45 +02:00
|
|
|
unsigned int *col_update_index = new unsigned int[nupdates];
|
2021-04-29 23:41:23 +02:00
|
|
|
read_int(file, group + "/col_update_index", col_update_index);
|
|
|
|
|
2021-05-10 14:50:45 +02:00
|
|
|
double *updates = new double[nupdates * dim];
|
2021-04-29 23:41:23 +02:00
|
|
|
read_double(file, group + "/updates", updates);
|
|
|
|
|
2021-05-10 14:50:45 +02:00
|
|
|
double *u = new double[nupdates * dim];
|
2021-04-29 23:41:23 +02:00
|
|
|
|
|
|
|
/* Test */
|
2021-06-21 14:29:24 +02:00
|
|
|
#ifdef DEBUG2
|
2021-04-29 23:41:23 +02:00
|
|
|
showMatrix(slater_matrix, dim, "OLD Slater");
|
|
|
|
#endif
|
|
|
|
|
2021-06-21 14:29:24 +02:00
|
|
|
#ifdef DEBUG2
|
2021-04-29 23:41:23 +02:00
|
|
|
showMatrix(slater_inverse, dim, "OLD Inverse");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (j = 0; j < nupdates; j++) {
|
|
|
|
for (i = 0; i < dim; i++) {
|
|
|
|
col = col_update_index[j];
|
2021-05-10 14:50:45 +02:00
|
|
|
u[i + j * dim] =
|
|
|
|
updates[i + j * dim] - slater_matrix[i * dim + (col - 1)];
|
|
|
|
slater_matrix[i * dim + (col - 1)] = updates[i + j * dim];
|
2021-04-29 23:41:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (version == "maponia3") {
|
|
|
|
MaponiA3(slater_inverse, dim, nupdates, u, col_update_index);
|
|
|
|
} else if (version == "sm1") {
|
|
|
|
SM1(slater_inverse, dim, nupdates, u, col_update_index);
|
|
|
|
} else if (version == "sm2") {
|
|
|
|
SM2(slater_inverse, dim, nupdates, u, col_update_index);
|
|
|
|
} else if (version == "sm3") {
|
|
|
|
SM3(slater_inverse, dim, nupdates, u, col_update_index);
|
2021-05-06 11:01:07 +02:00
|
|
|
} else if (version == "sm4") {
|
2021-05-06 10:51:42 +02:00
|
|
|
SM4(slater_inverse, dim, nupdates, u, col_update_index);
|
2021-04-29 23:41:23 +02:00
|
|
|
} else {
|
|
|
|
std::cerr << "Unknown version " << version << std::endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2021-06-21 14:29:24 +02:00
|
|
|
#ifdef DEBUG2
|
2021-04-29 23:41:23 +02:00
|
|
|
showMatrix(slater_matrix, dim, "NEW Slater");
|
|
|
|
#endif
|
|
|
|
|
2021-06-21 14:29:24 +02:00
|
|
|
#ifdef DEBUG2
|
2021-04-29 23:41:23 +02:00
|
|
|
showMatrix(slater_inverse, dim, "NEW Inverse");
|
|
|
|
#endif
|
|
|
|
|
2021-05-10 14:50:45 +02:00
|
|
|
double *res = new double[dim * dim]{0};
|
2021-04-29 23:41:23 +02:00
|
|
|
matMul(slater_matrix, slater_inverse, res, dim);
|
|
|
|
bool ok = is_identity(res, dim, 1e-3);
|
|
|
|
|
|
|
|
double res2 = residual2(res, dim);
|
2021-05-25 09:56:34 +02:00
|
|
|
double frob2 = norm_frobenius2(res, dim);
|
2021-04-29 23:41:23 +02:00
|
|
|
|
2021-06-21 14:29:24 +02:00
|
|
|
#ifdef DEBUG2
|
2021-04-29 23:41:23 +02:00
|
|
|
showMatrix(res, dim, "Result");
|
|
|
|
#endif
|
|
|
|
|
2021-05-25 09:56:34 +02:00
|
|
|
vfc_put_probe(probes, &(zero_padded_group)[0], &("frob2_" + version)[0],
|
|
|
|
frob2);
|
2021-05-03 13:59:43 +02:00
|
|
|
vfc_put_probe(probes, &(zero_padded_group)[0], &("res2_" + version)[0], res2);
|
2021-04-29 23:41:23 +02:00
|
|
|
|
2021-05-10 14:50:45 +02:00
|
|
|
delete[] res, updates, u, col_update_index, slater_matrix, slater_inverse;
|
2021-04-29 23:41:23 +02:00
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2021-06-17 16:50:42 +02:00
|
|
|
if (argc != 2) {
|
2021-04-29 23:41:23 +02:00
|
|
|
std::cerr << "Execute from within '/'" << std::endl;
|
2021-06-17 16:50:42 +02:00
|
|
|
std::cerr << "usage: vfc_test_h5 <path to cycles file>" << std::endl;
|
2021-04-29 23:41:23 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2021-06-17 16:50:42 +02:00
|
|
|
std::vector<int> cycles_list = get_cycles_list(argv[1]);
|
2021-04-29 23:41:23 +02:00
|
|
|
H5File file(FILE_NAME, H5F_ACC_RDONLY);
|
|
|
|
|
|
|
|
vfc_probes probes = vfc_init_probes();
|
|
|
|
probes = vfc_init_probes();
|
|
|
|
|
2021-06-17 16:50:42 +02:00
|
|
|
std::vector<std::string> versions = {"maponia3", "sm1", "sm2", "sm3", "sm4"};
|
|
|
|
|
2021-04-29 23:41:23 +02:00
|
|
|
bool ok;
|
|
|
|
for (int i = 0; i < cycles_list.size(); i++) {
|
2021-06-17 16:50:42 +02:00
|
|
|
|
2021-07-12 08:13:58 +02:00
|
|
|
for (const auto &version : versions) {
|
2021-06-17 16:50:42 +02:00
|
|
|
|
|
|
|
ok = test_cycle(file, cycles_list[i], version, &probes);
|
|
|
|
if (ok) {
|
|
|
|
std::cout << "ok -- cycle " << std::to_string(i) << std::endl;
|
|
|
|
} else {
|
|
|
|
std::cerr << "failed -- cycle " << std::to_string(i) << std::endl;
|
|
|
|
}
|
2021-04-29 23:41:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vfc_dump_probes(&probes);
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|