MilesCranmer commited on
Commit
d80a372
1 Parent(s): 7f9f476

Manual loop faster than broadcast

Browse files
Files changed (2) hide show
  1. julia/sr.jl +2 -2
  2. pysr/sr.py +18 -10
julia/sr.jl CHANGED
@@ -285,7 +285,7 @@ function evalTreeArray(tree::Node, cX::Array{Float32, 2})::Union{Array{Float32,
285
  return nothing
286
  end
287
  op_idx = tree.op
288
- UNAOP!(cumulator, op_idx)
289
  @inbounds for i=1:clen
290
  if isinf(cumulator[i]) || isnan(cumulator[i])
291
  return nothing
@@ -302,7 +302,7 @@ function evalTreeArray(tree::Node, cX::Array{Float32, 2})::Union{Array{Float32,
302
  return nothing
303
  end
304
  op_idx = tree.op
305
- BINOP!(cumulator, array2, op_idx)
306
  @inbounds for i=1:clen
307
  if isinf(cumulator[i]) || isnan(cumulator[i])
308
  return nothing
 
285
  return nothing
286
  end
287
  op_idx = tree.op
288
+ UNAOP!(cumulator, op_idx, clen)
289
  @inbounds for i=1:clen
290
  if isinf(cumulator[i]) || isnan(cumulator[i])
291
  return nothing
 
302
  return nothing
303
  end
304
  op_idx = tree.op
305
+ BINOP!(cumulator, array2, op_idx, clen)
306
  @inbounds for i=1:clen
307
  if isinf(cumulator[i]) || isnan(cumulator[i])
308
  return nothing
pysr/sr.py CHANGED
@@ -287,26 +287,34 @@ const limitPowComplexity = {"true" if limitPowComplexity else "false"}
287
  op_runner = ""
288
  if len(binary_operators) > 0:
289
  op_runner += """
290
- function BINOP!(x::Array{Float32, 1}, y::Array{Float32, 1}, i::Int)::Array{Float32, 1}
291
- if i == 1
292
- x .= """f"{binary_operators[0]}"".(x, y)"
 
 
293
  for i in range(1, len(binary_operators)):
294
  op_runner += f"""
295
- elseif i == {i+1}
296
- x .= {binary_operators[i]}.(x, y)"""
 
 
297
  op_runner += """
298
  end
299
  end"""
300
 
301
  if len(unary_operators) > 0:
302
  op_runner += """
303
- function UNAOP!(x::Array{Float32, 1}, i::Int)::Array{Float32, 1}
304
- if i == 1
305
- x .= """f"{unary_operators[0]}.(x)"
 
 
306
  for i in range(1, len(unary_operators)):
307
  op_runner += """
308
- elseif i == {i+1}
309
- x .= """f"{unary_operators[i]}.(x)"
 
 
310
  op_runner += """
311
  end
312
  end"""
 
287
  op_runner = ""
288
  if len(binary_operators) > 0:
289
  op_runner += """
290
+ @inline function BINOP!(x::Array{Float32, 1}, y::Array{Float32, 1}, i::Int, clen::Int)
291
+ if i === 1
292
+ @inbounds @simd for j=1:clen
293
+ x[j] = """f"{binary_operators[0]}""""(x[j], y[j])
294
+ end"""
295
  for i in range(1, len(binary_operators)):
296
  op_runner += f"""
297
+ elseif i === {i+1}
298
+ @inbounds @simd for j=1:clen
299
+ x[j] = {binary_operators[i]}(x[j], y[j])
300
+ end"""
301
  op_runner += """
302
  end
303
  end"""
304
 
305
  if len(unary_operators) > 0:
306
  op_runner += """
307
+ @inline function UNAOP!(x::Array{Float32, 1}, i::Int, clen::Int)
308
+ if i === 1
309
+ @inbounds @simd for j=1:clen
310
+ x[j] = """f"{unary_operators[0]}(x[j])""""
311
+ end"""
312
  for i in range(1, len(unary_operators)):
313
  op_runner += """
314
+ elseif i === {i+1}
315
+ @inbounds @simd for j=1:clen
316
+ x[j] = {unary_operators[i]}(x[j])
317
+ end"""
318
  op_runner += """
319
  end
320
  end"""