Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change of the function definition in example fails #14

Open
rachelglenn opened this issue Mar 25, 2019 · 3 comments
Open

change of the function definition in example fails #14

rachelglenn opened this issue Mar 25, 2019 · 3 comments

Comments

@rachelglenn
Copy link

Hi. I using the example with the integration
math.sin(x[0]+x[1]+x[2]+x[3])
I inserted an intermediate function: ( I checked that it can be evaluated outside of my_func(x))

def my_func(x):
yu = math.sin(x[0]+x[1]+x[2]+x[3])
integrand = P3_integrand_Eq2_c(x[0], x[1], x[2], x[3], wo, omegas, phase_type)
return yu

I get the following errors:

Process Process-2:
Traceback (most recent call last):
File "/home/ubuntu/anaconda3/lib/python3.6/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/home/ubuntu/anaconda3/lib/python3.6/multiprocessing/process.py", line 93, in run
self._target(*self._args, **self._kwargs)
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/ZMCintegral/ZMCintegral.py", line 111, in multi_processing
result.append(self.MCkernel(domain, i_batch))
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/ZMCintegral/ZMCintegral.py", line 373, in MCkernel
integration_kernel[blockspergrid, threadsperblock](num_loops, MCresult, chunk_size, n_chunk_x, domain, domain_range, batch_size, i_batch, rng_states, n_chunk)
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/numba/cuda/compiler.py", line 765, in call
kernel = self.specialize(*args)
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/numba/cuda/compiler.py", line 776, in specialize
kernel = self.compile(argtypes)
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/numba/cuda/compiler.py", line 792, in compile
**self.targetoptions)
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
return func(*args, **kwargs)
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/numba/cuda/compiler.py", line 62, in compile_kernel
cres = compile_cuda(pyfunc, types.void, args, debug=debug, inline=inline)
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
return func(*args, **kwargs)
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/numba/cuda/compiler.py", line 51, in compile_cuda
locals={})
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/numba/compiler.py", line 941, in compile_extra
return pipeline.compile_extra(func)
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/numba/compiler.py", line 372, in compile_extra
return self._compile_bytecode()
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/numba/compiler.py", line 872, in _compile_bytecode
return self._compile_core()
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/numba/compiler.py", line 859, in _compile_core
res = pm.run(self.status)
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
return func(*args, **kwargs)
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/numba/compiler.py", line 253, in run
raise patched_exception
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/numba/compiler.py", line 244, in run
stage()
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/numba/compiler.py", line 500, in stage_nopython_frontend
self.locals)
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/numba/compiler.py", line 1044, in type_inference_stage
infer.propagate()
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/numba/typeinfer.py", line 861, in propagate
raise errors[0]
numba.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of Function(<numba.cuda.compiler.DeviceFunctionTemplate object at 0x7fd0ffc81a90>) with argument(s) of type(s): (array(float64, 1d, C))

  • parameterized
    In definition 0:
    TypingError: Failed in nopython mode pipeline (step: nopython frontend)
    Untyped global name 'P3_integrand_Eq2_c': cannot determine Numba type of <class 'builtin_function_or_method'>

File "script.py", line 130:
def my_func(x):

yu = math.sin(x[0]+x[1]+x[2]+x[3])
integrand = P3_integrand_Eq2_c(x[0], x[1], x[2], x[3], wo, omegas, phase_type)
^

raised from /home/ubuntu/anaconda3/lib/python3.6/site-packages/numba/typeinfer.py:1180

In definition 1:
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Untyped global name 'P3_integrand_Eq2_c': cannot determine Numba type of <class 'builtin_function_or_method'>

File "script.py", line 130:
def my_func(x):

yu = math.sin(x[0]+x[1]+x[2]+x[3])
integrand = P3_integrand_Eq2_c(x[0], x[1], x[2], x[3], wo, omegas, phase_type)
^

raised from /home/ubuntu/anaconda3/lib/python3.6/site-packages/numba/typeinfer.py:1180

This error is usually caused by passing an argument of a type that is unsupported by the named function.
[1] During: resolving callee type: Function(<numba.cuda.compiler.DeviceFunctionTemplate object at 0x7fd0ffc81a90>)
[2] During: typing of call at /home/ubuntu/anaconda3/lib/python3.6/site-packages/ZMCintegral/ZMCintegral.py (365)

File "../../anaconda3/lib/python3.6/site-packages/ZMCintegral/ZMCintegral.py", line 365:
def integration_kernel(num_loops, MCresult, chunk_size, n_chunk_x, domain, domain_range, batch_size, i_batch, rng_states, n_chunk):

# feed in values to user defined function
cuda.atomic.add(MCresult, thread_id, fun(x_tuple))
^

Traceback (most recent call last):
File "script.py", line 139, in
result = MC.evaluate()
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/ZMCintegral/ZMCintegral.py", line 74, in evaluate
MCresult = self.importance_sampling_iteration(self.initial_domain, 0)
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/ZMCintegral/ZMCintegral.py", line 80, in importance_sampling_iteration
MCresult_chunks, large_std_chunk_id, MCresult_std_chunks = self.MCevaluate(domain)
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/ZMCintegral/ZMCintegral.py", line 130, in MCevaluate
MCresult.append(np.load(os.getcwd()+'/multi_temp/result'+str(i_batch)+'.npy'))
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/numpy/lib/npyio.py", line 372, in load
fid = open(file, "rb")
FileNotFoundError: [Errno 2] No such file or directory: '/home/ubuntu/workspace/phase_step/multi_temp/result0.npy'

@JCatesPH
Copy link
Contributor

JCatesPH commented Jun 5, 2019

Did you use the numba cuda decorator <@numba.cuda.jit> for your intermediate function?

I have also been working with this package, ZMCintegral, and found that all functions called must be decorated with 'jit'.

@Juenjie
Copy link
Collaborator

Juenjie commented Oct 5, 2019

the function P3_integrand_Eq2_c should also be a device function.

Here I provide you with an example similar to yours with our new version:

from ZMC.ZMCintegral_normal import MCintegral_normal
import math
import numpy as np

import time
start = time.time()

# user defined function
fun = """ 
import math
# define a device function that should be used by cuda kernel

@cuda.jit(device=True)
def my_intermediate_func(xx):
    return math.cos(xx)
    
@cuda.jit(device=True)
def fun(x):
    return math.sin(x[0]+x[1]+x[2]+x[3]+x[4]+x[5]+x[6]+my_intermediate_func(x[0]))
"""

# define arguments that MCintegral_normal requires
depth = 1
sigma_multiplier = 5
num_trials = 5
num_chunks_in_one_dimension = 12

# call MCintegral_normal
MC = MCintegral_normal(my_func = fun, 
                       domain = [[0,10],[0,10],[0,10],[0,10],[0,10],[0,10]], 
                       head_node_address = "210.45.78.43:1234",
                       depth = depth, 
                       sigma_multiplier = sigma_multiplier, 
                       num_trials = num_trials,
                       num_chunks_in_one_dimension = num_chunks_in_one_dimension)


# obtaining the result
result = MC.evaluate()


# print the formatted result
print('result = %s    std = %s' % (result[0], result[1]))
print('evaluation time {}'.format(time.time()-start))

@Juenjie
Copy link
Collaborator

Juenjie commented Oct 5, 2019

For constants, you have two choices:

  1. define the constans in the device function
# user defined function
fun = """ 
import math
# define a device function that should be used by cuda kernel

@cuda.jit(device=True)
def my_intermediate_func(xx, constant):
    return math.cos(xx+constant)
    
@cuda.jit(device=True)
def fun(x):
    constant = 1
    return math.sin(x[0]+x[1]+x[2]+x[3]+x[4]+x[5]+x[6]+my_intermediate_func(x[0]+constant))
"""
  1. Use the new functionality of parameter scan
from ZMC.ZMCintegral_functional import MCintegral_functional
import math
import numpy as np

import time
start = time.time()


# user defined function
fun = """ 
import math
# define a device function that should be used by cuda kernel
@cuda.jit(device=True)
def fun(x,para):
    return math.sin(x[0]+x[1]+x[2]+x[3]+para[0]+para[1])
"""

# para contains two parameters
para = [[1,2,3,4,5],[1.1,2.2,3.1]]


# sample points is taken to 10**6
sample_points = 10**6

# the parameter grid has totally 5*3=15 points
# we choose 3 batches as an example
batch_size = 5

# call MCintegral_functional
MC = MCintegral_functional(my_func = fun, 
                           domain = [[0,1],[0,1],[0,1],[0,1]], 
                           parameters = para, 
                           head_node_address = "210.45.78.19:1234",
                           num_points = sample_points, 
                           batch_size = batch_size)

# obtaining the result
result = MC.evaluate()
result = print(np.reshape(result,[3,5]))
print('evaluation time {}'.format(time.time()-start))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants