Skip to content

Commit

Permalink
Fix issue#9
Browse files Browse the repository at this point in the history
  • Loading branch information
Lugatod committed Feb 26, 2017
1 parent 58ddda2 commit db2b264
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 27 deletions.
27 changes: 4 additions & 23 deletions src/backends/module.cpp
Expand Up @@ -7,34 +7,15 @@
#include "module.h"
#include "util/strings.h"
#include "error.h"
#include "util/env.h"

using namespace std;

namespace taco {
namespace ir {

namespace {

string get_from_env(string flag, string dflt) {
char const *ret = getenv(flag.c_str());
if (!ret) {
return dflt;
} else {
return string(ret);
}
}

}

void Module::setJITTmpdir() {
// use POSIX logic for finding a temp dir
auto tmp = get_from_env("TMPDIR", "/tmp/");

uassert(access(tmp.c_str(), W_OK) == 0) <<
"Unable to write to temporary directory for code generation. "
"Please set the environment variable TMPDIR to somewhere writable";

tmpdir = tmp;
tmpdir = util::getTmpdir();
}

void Module::setJITLibname() {
Expand Down Expand Up @@ -69,8 +50,8 @@ string Module::compile() {
string prefix = tmpdir+libname;
string fullpath = prefix + ".so";

string cc = get_from_env("TACO_CC", "cc");
string cflags = get_from_env("TACO_CFLAGS",
string cc = util::getFromEnv("TACO_CC", "cc");
string cflags = util::getFromEnv("TACO_CFLAGS",
"-O3 -ffast-math -std=c99") + " -shared -fPIC";

string cmd = cc + " " + cflags + " " +
Expand Down
36 changes: 36 additions & 0 deletions src/util/env.h
@@ -0,0 +1,36 @@
#ifndef SRC_UTIL_ENV_H_
#define SRC_UTIL_ENV_H_

#include <string>
#include <unistd.h>

#include "error.h"

namespace taco {
namespace util {
std::string getFromEnv(std::string flag, std::string dflt);
std::string getTmpdir();

std::string getFromEnv(std::string flag, std::string dflt) {
char const *ret = getenv(flag.c_str());
if (!ret) {
return dflt;
} else {
return std::string(ret);
}
}

std::string getTmpdir() {
// use POSIX logic for finding a temp dir
auto tmpdir = getFromEnv("TMPDIR", "/tmp/");

uassert(access(tmpdir.c_str(), W_OK) == 0) <<
"Unable to write to temporary directory for code generation. "
"Please set the environment variable TMPDIR to somewhere writable";

return tmpdir;
}

}}

#endif /* SRC_UTIL_ENV_H_ */
12 changes: 8 additions & 4 deletions test/api-tests.cpp
Expand Up @@ -10,6 +10,7 @@
#include "expr_nodes.h"
#include "storage/storage.h"
#include "operator.h"
#include "util/env.h"

#include <cmath>
#include <stdio.h>
Expand All @@ -24,6 +25,7 @@ typedef std::vector<IndexType> IndexArray; // Index values
typedef std::vector<IndexArray> Index; // [0,2] index arrays per Index
typedef std::vector<Index> Indices; // One Index per level


struct APIStorage {
APIStorage(Tensor<double> tensor,
const Indices& expectedIndices,
Expand Down Expand Up @@ -130,8 +132,9 @@ TEST_P(apiwhb, api) {

if (tensor.getFormat().isCSC()) {
std::string testdir=TOSTRING(TACO_TEST_DIR);
auto tmpdir = util::getTmpdir();
std::string datafilename=testdir + "/data/" + GetParam().filename;
std::string CSCfilename=GetParam().filename+".csc";
std::string CSCfilename=tmpdir + GetParam().filename + ".csc";
tensor.writeHB(CSCfilename);
std::string diffcommand="diff -wB <(tail -n +3 " + CSCfilename + " ) <(tail -n +3 " + datafilename + " ) > diffresult ";
std::ofstream diffcommandfile;
Expand All @@ -156,8 +159,9 @@ TEST_P(apiwmtx, api) {

if (tensor.getFormat().isCSC()) {
std::string testdir=TOSTRING(TACO_TEST_DIR);
auto tmpdir = util::getTmpdir();
std::string datafilename=testdir + "/data/" + GetParam().filename;
std::string MTXfilename=GetParam().filename+".mtx";
std::string MTXfilename=tmpdir + GetParam().filename + ".mtx";
tensor.writeMTX(MTXfilename);
std::string diffcommand="diff -wB -I '^%.*' " + MTXfilename + " " + datafilename + " > diffresult ";
std::ofstream diffcommandfile;
Expand All @@ -167,8 +171,8 @@ TEST_P(apiwmtx, api) {
system("chmod +x diffcommand.tac ; bash ./diffcommand.tac ");
std::ifstream diffresult("diffresult");
bool nodiff=(diffresult.peek() == std::ifstream::traits_type::eof());
// std::string cleancommand="rm diffresult diffcommand.tac "+MTXfilename;
// system(cleancommand.c_str());
std::string cleancommand="rm diffresult diffcommand.tac "+MTXfilename;
system(cleancommand.c_str());
ASSERT_TRUE(nodiff);
}
}
Expand Down

0 comments on commit db2b264

Please sign in to comment.