Commit 587f7c6c authored by Katya Abramushkina's avatar Katya Abramushkina
Browse files

Add a mutex to the Profiler class

The values of the variables can be changed before the logger message is printed. To avoid this error, a mutex was added
parent 351bbcf5
......@@ -56,7 +56,7 @@ sub main()
cf get_res[0][0]: copy_fragment(enf, tmp[0][0][0], res[0][0][nf-1]);
}
cf print_res[0][0]: print_fragment(enf, res[0][0][nf-1], "Result fragment", 0, 0);
cf print_res[0][0]: print_fragment(enf, res[0][0][nf-1], "Result fragment", 0, 0) @ { delete res[0][0][0]; };
// for i = 0..nf-1 for k = 0..nf-1
// {
......
#include <cstdio>
#include <memory.h>
#include <unistd.h>
#include "ucenv.h"
extern "C"
{
void c_set_int_value(int value, const char *varname, OutputDF &df)
{
df.setValue<int>(value);
printf("c_set_int_value: %s = %d\n", varname, value);
}
void c_copy_fragment(int size, const InputDF &idf, OutputDF &odf)
{
const int *idf_data = idf.getData<int>();
int *odf_data = odf.create<int>(size * size);
for (int i = 0; i < size * size; i++) {
odf_data[i] = idf_data[i];
}
}
void c_init_fragment(int size, int coef, OutputDF &odf)
{
int *data = odf.create<int>(size * size);
for (int i = 0; i < size * size; i++) {
data[i] = i * coef;
}
}
void c_multiply_fragments(int size,
const InputDF &adf,
const InputDF &bdf,
OutputDF &odf)
{
const int *a = adf.getData<int>();
const int *b = bdf.getData<int>();
int *c = odf.create<int>(size * size);
memset(c, 0, size * size * sizeof(int));
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
for (int k = 0; k < size; k++) {
c[i * size + k] += a[i * size + j] * b[j * size + k];
}
}
}
}
void c_print_fragment(int size,
const InputDF &idf,
const char *varname,
int x,
int y)
{
printf("c_print_matrix_fragment: %s[%d][%d]\n", varname, x, y);
const int *data = idf.getData<int>();
for (int i = 0; i < size; i++) {
for (int k = 0; k < size; k++) {
printf("[%d][%d] %d\t", i, k, data[i * size + k]);
}
printf("\n");
sleep(1);
}
}
void c_sum_fragments(int size,
const InputDF &adf,
const InputDF &bdf,
OutputDF &odf)
{
const int *a = adf.getData<int>();
const int *b = bdf.getData<int>();
int *c = odf.create<int>(size * size);
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
c[i * size + j] = a[i * size + j] + b[i * size + j];
}
}
}
}
#include <cstdio>
#include <memory.h>
#include <unistd.h>
#include "ucenv.h"
#include "profiler.h"
extern "C"
{
StdoutLogger logger;
void c_set_int_value(int value, const char *varname, OutputDF &df)
{
df.setValue<int>(value);
// printf("c_set_int_value: %s = %d\n", varname, value);
}
void c_copy_fragment(int size, const InputDF &idf, OutputDF &odf)
{
const int *idf_data = idf.getData<int>();
int *odf_data = odf.create<int>(size * size);
// TODO: log event -> eventType (class enum)
// submitEvent(CF, DF, EventType = MemoryAllocation);
// submitEvent(EventType =);
logger.log_memory_usage(size * size, odf.getName());
//
for (int i = 0; i < size * size; i++) {
odf_data[i] = idf_data[i];
}
}
void c_init_fragment(int size, int coef, OutputDF &odf)
{
int *data = odf.create<int>(size * size);
//
logger.log_memory_usage(size * size, odf.getName());
//
for (int i = 0; i < size * size; i++) {
data[i] = i * coef;
}
}
void c_multiply_fragments(int size,
const InputDF &adf,
const InputDF &bdf,
OutputDF &odf)
{
const int *a = adf.getData<int>();
const int *b = bdf.getData<int>();
int *c = odf.create<int>(size * size);
//
logger.log_memory_usage(size * size, odf.getName());
//
memset(c, 0, size * size * sizeof(int));
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
for (int k = 0; k < size; k++) {
c[i * size + k] += a[i * size + j] * b[j * size + k];
}
}
}
}
void c_print_fragment(int size,
const InputDF &idf,
const char *varname,
int x,
int y)
{
printf("c_print_matrix_fragment: %s[%d][%d]\n", varname, x, y);
const int *data = idf.getData<int>();
for (int i = 0; i < size; i++) {
for (int k = 0; k < size; k++) {
printf("[%d][%d] %d\t", i, k, data[i * size + k]);
}
printf("\n");
sleep(1);
}
}
void c_sum_fragments(int size,
const InputDF &adf,
const InputDF &bdf,
OutputDF &odf)
{
const int *a = adf.getData<int>();
const int *b = bdf.getData<int>();
int *c = odf.create<int>(size * size);
//
logger.log_memory_usage(size * size, odf.getName());
//
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
c[i * size + j] = a[i * size + j] + b[i * size + j];
}
}
}
}
#pragma once
#include <chrono>
#include <ctime>
#include <fstream>
#include <iostream>
#include <mutex>
class Logger
{
public:
Logger() : start_(std::chrono::system_clock::now()) {}
virtual ~Logger() = default;
std::string time()
{
const auto end = std::chrono::system_clock::now();
std::chrono::duration<double> seconds = end - start_;
const std::string time_message = std::to_string(seconds.count()) + "\n";
return time_message;
}
protected:
template<typename... Args>
void write(std::ostream &stream, Args &&...args)
{
(stream << ... << args) << '\n' << '\n';
}
void write_prefix(std::ostream &stream)
{
stream << "[ LOG ] Time since started:: " << time() << "\t";
}
void write_error_prefix(std::ostream &stream)
{
stream << "[ERROR] Time since started:: " << time()
<< "An error occurred::\n\t";
}
std::mutex m_;
private:
const std::chrono::time_point<std::chrono::system_clock> start_;
// mutable std::mutex m_;
// std::unique_lock<std::mutex> lk(m_);
}; // class Logger
class StdoutLogger : public Logger
{
public:
StdoutLogger() { bytesInTotal = 0; }
template<typename... Rest>
void log(const Rest &...rest)
{
write_prefix(std::cout);
write(std::cout, rest...);
}
template<typename... Rest>
void log_error(const Rest &...rest)
{
write_error_prefix(std::cout);
write(std::cout, rest...);
}
template<typename T>
friend StdoutLogger &operator<<(StdoutLogger &logger, const T &Arg)
{
std::cout << Arg;
return logger;
}
void log_memory_usage(size_t bytes, std::string dFName)
{
std::unique_lock<std::mutex> lk(m_);
bytesInTotal += bytes;
log("Allocated ", bytes, " bytes for ", dFName,
". Total: ", bytesInTotal);
}
private:
size_t bytesInTotal;
}; // class StdoutLogger
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment