forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.jl
408 lines (363 loc) · 9.93 KB
/
util.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# timing
time() = ccall(:clock_now, Float64, ())
function tic()
t0 = time()
tls(:TIMERS, (t0, get(tls(), :TIMERS, ())))
return t0
end
function toq()
t1 = time()
timers = get(tls(), :TIMERS, ())
if is(timers,())
error("toc() without tic()")
end
t0 = timers[1]
tls(:TIMERS, timers[2])
t1-t0
end
function toc()
t = toq()
println("elapsed time: ", t, " seconds")
return t
end
# high-resolution time
# returns in nanoseconds
function time_ns()
return ccall(:jl_hrtime, Uint64, ())
end
# print elapsed time, return expression value
macro time(ex)
quote
local t0 = time()
local val = $(esc(ex))
local t1 = time()
println("elapsed time: ", t1-t0, " seconds")
val
end
end
# print nothing, return elapsed time
macro elapsed(ex)
quote
local t0 = time()
local val = $(esc(ex))
time()-t0
end
end
function peakflops()
a = rand(2000,2000)
t = @elapsed a*a
t = @elapsed a*a
floprate = (2*2000.0^3/t)
println("The peak flop rate is ", floprate*1e-9, " gigaflops")
floprate
end
# source files, editing, function reflection
function function_loc(f::Function, types)
for m = methods(f, types)
if isa(m[3],LambdaStaticData)
lsd = m[3]::LambdaStaticData
ln = lsd.line
if ln > 0
return (find_in_path(string(lsd.file)), ln)
end
end
end
error("could not find function definition")
end
function_loc(f::Function) = function_loc(f, (Any...))
function whicht(f, types)
for m = methods(f, types)
if isa(m[3],LambdaStaticData)
lsd = m[3]::LambdaStaticData
d = f.env.defs
while !is(d,())
if is(d.func.code, lsd)
print(stdout_stream, f.env.name)
show(stdout_stream, d); println(stdout_stream)
return
end
d = d.next
end
end
end
end
which(f, args...) = whicht(f, map(a->(isa(a,Type) ? Type{a} : typeof(a)), args))
edit(file::String) = edit(file, 1)
function edit(file::String, line::Int)
editor = get(ENV, "JULIA_EDITOR", "emacs")
issrc = file[end-2:end] == ".jl"
if issrc
if file[1]!='/' && !is_file_readable(file)
file2 = "$JULIA_HOME/base/$file"
if is_file_readable(file2)
file = file2
end
end
if editor == "emacs"
jmode = "$JULIA_HOME/contrib/julia-mode.el"
run(`emacs $file --eval "(progn
(require 'julia-mode \"$jmode\")
(julia-mode)
(goto-line $line))"`)
elseif editor == "vim"
run(`vim $file +$line`)
elseif editor == "textmate"
run(`mate $file -l $line`)
elseif editor == "subl"
run(`subl $file:$line`)
else
error("Invalid JULIA_EDITOR value: $(repr(editor))")
end
else
if editor == "emacs"
run(`emacs $file --eval "(goto-line $line)"`)
elseif editor == "vim"
run(`vim $file +$line`)
elseif editor == "textmate"
run(`mate $file -l $line`)
elseif editor == "subl"
run(`subl $file:$line`)
else
error("Invalid JULIA_EDITOR value: $(repr(editor))")
end
end
nothing
end
edit(file::String) = edit(file, 1)
function less(file::String, line::Int)
pager = get(ENV, "PAGER", "less")
run(`$pager +$(line)g $file`)
end
less(file::String) = less(file, 1)
edit(f::Function) = edit(function_loc(f)...)
edit(f::Function, t) = edit(function_loc(f,t)...)
less(f::Function) = less(function_loc(f)...)
less(f::Function, t) = less(function_loc(f,t)...)
disassemble(f::Function, types::Tuple) =
print(ccall(:jl_dump_function, Any, (Any,Any), f, types)::ByteString)
function methods(f::Function)
if !isgeneric(f)
error("methods: error: not a generic function")
end
f.env
end
methods(t::CompositeKind) = (methods(t,Tuple); # force constructor creation
t.env)
# require
# Store list of files and their load time
global _jl_package_list = Dict{ByteString,Float64}()
require(fname::String) = require(bytestring(fname))
require(f::String, fs::String...) = (require(f); for x in fs require(x); end)
function require(name::ByteString)
path = find_in_path(name)
if !has(_jl_package_list,path)
load_now(name)
else
# Determine whether the file has been modified since it was last loaded
if mtime(path) > _jl_package_list[path]
load_now(name)
end
end
end
const load = require
# remote/parallel load
include_string(txt::ByteString) = ccall(:jl_load_file_string, Void, (Ptr{Uint8},), txt)
function is_file_readable(path)
local f
try
f = open(bytestring(path))
catch
return false
end
close(f)
return true
end
function find_in_path(fname)
if fname[1] == '/'
return realpath(fname)
end
for pfx in LOAD_PATH
if pfx != "" && pfx[end] != '/'
pfxd = strcat(pfx,"/",fname)
else
pfxd = strcat(pfx,fname)
end
if is_file_readable(pfxd)
return realpath(pfxd)
end
end
return realpath(fname)
end
begin
local in_load = false
local in_remote_load = false
local load_dict = {}
global load_now, remote_load
load_now(fname::String) = load_now(bytestring(fname))
function load_now(fname::ByteString)
if in_load
path = find_in_path(fname)
push(load_dict, fname)
f = open(path)
push(load_dict, readall(f))
close(f)
include(path)
_jl_package_list[path] = time()
return
elseif in_remote_load
for i=1:2:length(load_dict)
if load_dict[i] == fname
return include_string(load_dict[i+1])
end
end
else
in_load = true
iserr, err = false, ()
try
ccall(:jl_register_toplevel_eh, Void, ())
load_now(fname)
for p = 1:nprocs()
if p != myid()
remote_do(p, remote_load, load_dict)
end
end
catch e
iserr, err = true, e
end
load_dict = {}
in_load = false
if iserr throw(err); end
end
end
function remote_load(dict)
load_dict = dict
in_remote_load = true
try
load_now(dict[1])
catch e
in_remote_load = false
throw(e)
end
in_remote_load = false
nothing
end
end
evalfile(fname::String) = eval(parse(readall(fname))[1])
# help
_jl_help_category_list = nothing
_jl_help_category_dict = nothing
_jl_help_function_dict = nothing
function _jl_init_help()
global _jl_help_category_list, _jl_help_category_dict, _jl_help_function_dict
if _jl_help_category_dict == nothing
println("Loading help data...")
helpdb = evalfile("$JULIA_HOME/../lib/julia/helpdb.jl")
_jl_help_category_list = {}
_jl_help_category_dict = Dict()
_jl_help_function_dict = Dict()
for (cat,func,desc) in helpdb
if !has(_jl_help_category_dict, cat)
push(_jl_help_category_list, cat)
_jl_help_category_dict[cat] = {}
end
push(_jl_help_category_dict[cat], func)
if !has(_jl_help_function_dict, func)
_jl_help_function_dict[func] = {}
end
push(_jl_help_function_dict[func], desc)
end
end
end
function help()
_jl_init_help()
print(
" Welcome to Julia. The full manual is available at
http://docs.julialang.org
To get help on a function, try help(function). To search all help text,
try apropos(\"string\"). To see available functions, try help(category),
for one of the following categories:
")
for cat = _jl_help_category_list
if !isempty(_jl_help_category_dict[cat])
print(" ")
show(cat); println()
end
end
end
function help(cat::String)
_jl_init_help()
if !has(_jl_help_category_dict, cat)
# if it's not a category, try another named thing
return help_for(cat)
end
println("Help is available for the following items:")
for func = _jl_help_category_dict[cat]
print(func, " ")
end
println()
end
function _jl_print_help_entries(entries)
first = true
for desc in entries
if !first
println()
end
println(strip(desc))
first = false
end
end
help_for(s::String) = help_for(s, 0)
function help_for(fname::String, obj)
_jl_init_help()
if has(_jl_help_function_dict, fname)
_jl_print_help_entries(_jl_help_function_dict[fname])
else
if isgeneric(obj)
repl_show(obj); println()
else
println("No help information found.")
end
end
end
function apropos(txt::String)
_jl_init_help()
n = 0
r = Regex("\\Q$txt", PCRE.CASELESS)
first = true
for (cat, _) in _jl_help_category_dict
if ismatch(r, cat)
println("Category: \"$cat\"")
first = false
end
end
for (func, entries) in _jl_help_function_dict
if ismatch(r, func) || anyp(e->ismatch(r,e), entries)
if !first
println()
end
_jl_print_help_entries(entries)
first = false
n+=1
end
end
if n == 0
println("No help information found.")
end
end
function help(f::Function)
if is(f,help)
return help()
end
help_for(string(f), f)
end
help(t::CompositeKind) = help_for(string(t.name),t)
function help(x)
show(x)
t = typeof(x)
println(" is of type $t")
if isa(t,CompositeKind)
println(" which has fields $(t.names)")
end
end
# misc
times(f::Function, n::Int) = for i=1:n f() end