From 6e25ad2dc80ee21ad94ef57ae70cbcb331cf5d1c Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Thu, 31 Mar 2016 13:11:38 -0700 Subject: Separate sys.COLORS and sys.fpath into their own files The sys package forks processes when the package is required to determine the OS and prefix. Unfortunately, due to a bug in OpenBLAS, simulatenous calls to fork can deadlock. The torch7 and image packages require sys at intialization, which now makes deadlocks much more common when using Torch threads and OpenBLAS. This moves sys.COLORS and sys.fpath into their own files which can be required independently, so that torch7 and image don't have to require all of sys and fork at initialization. --- CMakeLists.txt | 2 +- colors.lua | 32 ++++++++++++++++++++++++++++++++ fpath.lua | 10 ++++++++++ init.lua | 35 +++-------------------------------- 4 files changed, 46 insertions(+), 33 deletions(-) create mode 100644 colors.lua create mode 100644 fpath.lua diff --git a/CMakeLists.txt b/CMakeLists.txt index e4a6db3..baa0309 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ CMAKE_POLICY(VERSION 2.6) FIND_PACKAGE(Torch REQUIRED) SET(src sys.c) -SET(luasrc init.lua) +SET(luasrc init.lua colors.lua fpath.lua) ADD_TORCH_PACKAGE(sys "${src}" "${luasrc}") diff --git a/colors.lua b/colors.lua new file mode 100644 index 0000000..f6543e5 --- /dev/null +++ b/colors.lua @@ -0,0 +1,32 @@ +-------------------------------------------------------------------------------- +-- colors, can be used to print things in color +-------------------------------------------------------------------------------- +local colors = { + none = '\27[0m', + black = '\27[0;30m', + red = '\27[0;31m', + green = '\27[0;32m', + yellow = '\27[0;33m', + blue = '\27[0;34m', + magenta = '\27[0;35m', + cyan = '\27[0;36m', + white = '\27[0;37m', + Black = '\27[1;30m', + Red = '\27[1;31m', + Green = '\27[1;32m', + Yellow = '\27[1;33m', + Blue = '\27[1;34m', + Magenta = '\27[1;35m', + Cyan = '\27[1;36m', + White = '\27[1;37m', + _black = '\27[40m', + _red = '\27[41m', + _green = '\27[42m', + _yellow = '\27[43m', + _blue = '\27[44m', + _magenta = '\27[45m', + _cyan = '\27[46m', + _white = '\27[47m' +} + +return colors diff --git a/fpath.lua b/fpath.lua new file mode 100644 index 0000000..9e4854c --- /dev/null +++ b/fpath.lua @@ -0,0 +1,10 @@ +-------------------------------------------------------------------------------- +-- always returns the path of the file running +-------------------------------------------------------------------------------- +local function fpath() + local fpath = _G.debug.getinfo(2).source:gsub('@','') + if fpath:find('/') ~= 1 then fpath = paths.concat(paths.cwd(),fpath) end + return paths.dirname(fpath),paths.basename(fpath) +end + +return fpath diff --git a/init.lua b/init.lua index 590975f..4596a92 100644 --- a/init.lua +++ b/init.lua @@ -78,8 +78,7 @@ function sys.uname() end end end -local _, os = pcall(sys.uname) -sys.OS = os +sys.OS = sys.uname() -------------------------------------------------------------------------------- -- ls (list dir) @@ -97,11 +96,7 @@ sys.prefix = execute('which lua'):gsub('//','/'):gsub('/bin/lua\n','') -------------------------------------------------------------------------------- -- always returns the path of the file running -------------------------------------------------------------------------------- -function sys.fpath() - local fpath = _G.debug.getinfo(2).source:gsub('@','') - if fpath:find('/') ~= 1 then fpath = paths.concat(paths.cwd(),fpath) end - return paths.dirname(fpath),paths.basename(fpath) -end +sys.fpath = require 'sys.fpath' -------------------------------------------------------------------------------- -- split string based on pattern pat @@ -143,31 +138,7 @@ end -------------------------------------------------------------------------------- -- colors, can be used to print things in color -------------------------------------------------------------------------------- -sys.COLORS = {none = '\27[0m', - black = '\27[0;30m', - red = '\27[0;31m', - green = '\27[0;32m', - yellow = '\27[0;33m', - blue = '\27[0;34m', - magenta = '\27[0;35m', - cyan = '\27[0;36m', - white = '\27[0;37m', - Black = '\27[1;30m', - Red = '\27[1;31m', - Green = '\27[1;32m', - Yellow = '\27[1;33m', - Blue = '\27[1;34m', - Magenta = '\27[1;35m', - Cyan = '\27[1;36m', - White = '\27[1;37m', - _black = '\27[40m', - _red = '\27[41m', - _green = '\27[42m', - _yellow = '\27[43m', - _blue = '\27[44m', - _magenta = '\27[45m', - _cyan = '\27[46m', - _white = '\27[47m'} +sys.COLORS = require 'sys.colors' -------------------------------------------------------------------------------- -- backward compat -- cgit v1.2.3