def wrapper(*args): if len(args) != expected_arguments_number: raise ValueError("{0} syswow accept {1} args ({2} given)".format(target.__name__, expected_arguments_number, len(args))) # Transform args (ctypes byref possibly) to int writable_args = [] for i, value in enumerate(args): if not isinstance(value, (int, long)): try: value = ctypes.cast(value, ctypes.c_void_p).value except ctypes.ArgumentError as e: raise ctypes.ArgumentError("Argument {0}: wrong type <{1}>".format(i, type(value).__name__)) writable_args.append(value) # Build buffer buffer = struct.pack("<" + "Q" * len(writable_args), *writable_args) ctypes.memmove(argument_buffer, buffer, len(buffer)) # Copy origincal args in function, for errcheck if needed native_caller.current_original_args = args # TODO: THIS IS NOT THREAD SAFE return native_caller()
def apply(self, **kwargs): """ Execute the Operator. With no arguments provided, the Operator runs using the data carried by the objects appearing in the input expressions -- these are referred to as the "default arguments". Optionally, any of the Operator default arguments may be replaced by passing suitable key-value arguments. Given ``apply(k=v, ...)``, ``(k, v)`` may be used to: * replace a Constant. In this case, ``k`` is the name of the Constant, ``v`` is either a Constant or a scalar value. * replace a Function (SparseFunction). Here, ``k`` is the name of the Function, ``v`` is either a Function or a numpy.ndarray. * alter the iteration interval along a Dimension. Consider a generic Dimension ``d`` iterated over by the Operator. By default, the Operator runs over all iterations within the compact interval ``[d_m, d_M]``, where ``d_m`` and ``d_M`` are, respectively, the smallest and largest integers not causing out-of-bounds memory accesses (for the Grid Dimensions, this typically implies iterating over the entire physical domain). So now ``k`` can be either ``d_m`` or ``d_M``, while ``v`` is an integer value. Examples -------- Consider the following Operator >>> from devito import Eq, Grid, TimeFunction, Operator >>> grid = Grid(shape=(3, 3)) >>> u = TimeFunction(name='u', grid=grid, save=3) >>> op = Operator(Eq(u.forward, u + 1)) The Operator is run by calling ``apply`` >>> summary = op.apply() The variable ``summary`` contains information about runtime performance. As no key-value parameters are specified, the Operator runs with its default arguments, namely ``u=u, x_m=0, x_M=2, y_m=0, y_M=2, time_m=0, time_M=1``. At this point, the same Operator can be used for a completely different run, for example >>> u2 = TimeFunction(name='u', grid=grid, save=5) >>> summary = op.apply(u=u2, x_m=1, y_M=1) Now, the Operator will run with a different set of arguments, namely ``u=u2, x_m=1, x_M=2, y_m=0, y_M=1, time_m=0, time_M=3``. To run an Operator that only uses buffered TimeFunctions, the maximum iteration point along the time dimension must be explicitly specified (otherwise, the Operator wouldn't know how many iterations to run). >>> u3 = TimeFunction(name='u', grid=grid) >>> op = Operator(Eq(u3.forward, u3 + 1)) >>> summary = op.apply(time_M=10) """ # Build the arguments list to invoke the kernel function args = self.arguments(**kwargs) # Invoke kernel function with args arg_values = [args[p.name] for p in self.parameters] try: self.cfunction(*arg_values) except ctypes.ArgumentError as e: if e.args[0].startswith("argument "): argnum = int(e.args[0][9:].split(':')[0]) - 1 newmsg = "error in argument '%s' with value '%s': %s" % ( self.parameters[argnum].name, arg_values[argnum], e.args[0]) raise ctypes.ArgumentError(newmsg) from e else: raise # Post-process runtime arguments self._postprocess_arguments(args, **kwargs) # Output summary of performance achieved return self._profile_output(args)
def apply(self, **kwargs): """ Run the operator. Without additional parameters specified, the operator runs on the same data objects used to build it -- the so called ``default arguments``. Optionally, any of the operator default arguments may be replaced by passing suitable key-value parameters. Given ``apply(k=v, ...)``, ``(k, v)`` may be used to: :: * replace a constant (scalar) used by the operator. In this case, ``k`` is the name of the constant; ``v`` is either an object of type :class:`Constant` or an actual scalar value. * replace a function (tensor) used by the operator. In this case, ``k`` is the name of the function; ``v`` is either an object of type :class:`TensorFunction` or a :class:`numpy.ndarray`. * alter the iteration interval along a given :class:`Dimension` ``d``, which represents a subset of the operator iteration space. By default, the operator runs over all iterations within the compact interval ``[d_m, d_M]``, in which ``d_m`` and ``d_M`` are, respectively, the smallest and largest integers not causing out-of-bounds memory accesses. In this case, ``k`` can be any of ``(d_m, d_M, d_n)``; ``d_n`` can be used to indicate to run for exactly ``n`` iterations starting at ``d_m``. ``d_n`` is ignored (raising a warning) if ``d_M`` is also provided. ``v`` is an integer value. Examples -------- The following operator implements a trivial time-marching method which adds 1 to every grid point at every time iteration. >>> from devito import Eq, Grid, TimeFunction, Operator >>> grid = Grid(shape=(3, 3)) >>> u = TimeFunction(name='u', grid=grid, save=3) >>> op = Operator(Eq(u.forward, u + 1)) The operator is run by calling >>> op.apply() As no key-value parameters are specified, the operator runs with its default arguments, namely ``u=u, x_m=0, x_M=2, y_m=0, y_M=2, time_m=0, time_M=1``. Note that one can access the operator dimensions via the ``grid`` object (e.g., ``grid.dimensions`` for the ``x`` and ``y`` space dimensions). At this point, the same operator can be used for a completely different run, for example >>> u2 = TimeFunction(name='u', grid=grid, save=5) >>> op.apply(u=u2, x_m=1, y_M=1) Now, the operator will run with a different set of arguments, namely ``u=u2, x_m=1, x_M=2, y_m=0, y_M=1, time_m=0, time_M=3``. To run an operator that only uses buffered :class:`TimeFunction`s, the maximum iteration point along the time dimension must be explicitly specified (otherwise, the operator wouldn't know how many iterations to run). >>> u3 = TimeFunction(name='u', grid=grid) >>> op = Operator(Eq(u3.forward, u3 + 1)) >>> op.apply(time_M=10) """ # Build the arguments list to invoke the kernel function args = self.arguments(**kwargs) # Invoke kernel function with args arg_values = [args[p.name] for p in self.parameters] try: self.cfunction(*arg_values) except ctypes.ArgumentError as e: if e.args[0].startswith("argument "): argnum = int(e.args[0][9:].split(':')[0]) - 1 newmsg = "error in argument '%s' with value '%s': %s" % ( self.parameters[argnum].name, arg_values[argnum], e.args[0]) raise ctypes.ArgumentError(newmsg) from e else: raise # Post-process runtime arguments self._postprocess_arguments(args, **kwargs) # Output summary of performance achieved return self._profile_output(args)
def deal(self): long_order, short_order = self.pair_queue.get_first_order() if not long_order: # self.logger.info("No Order Now") return False if not short_order: # self.pair_queue.push(long_order) # self.logger.info("No Order Now") return False a = ctypes.c_double(100000.0) print('+++') print(self.close_price) b = ctypes.c_double(self.close_price) dll = ctypes.CDLL('./deal/libdeal.so') dll.Deal.restype = exchange dll.Deal.argtypes = [ stock,stock, ctypes.c_double, ctypes.c_double ] # converted_long_order = order_conversion(long_order) # converted_short_order = order_conversion(short_order) # # print(converted_long_order.buy_id) # result = dll.Deal(converted_long_order, converted_short_order, a, b) # print(result.volume) # self.save_deal(result, long_order, short_order) # re_order = regenerate_order(result, long_order, short_order) # if re_order: # self.pair_queue.push(re_order) # else: # pass # self.logger.info("Success") try: converted_long_order = order_conversion(long_order) converted_short_order = order_conversion(short_order) print(long_order.volume) result = dll.Deal(converted_long_order,converted_short_order,a,b) if int(result.volume) == 0: raise ctypes.ArgumentError('deal fail') print("finish") print(result.volume) self.save_deal(result,long_order,short_order) re_order = regenerate_order(result,long_order,short_order) if re_order: print("re_order") print(re_order) self.pair_queue.push(re_order) else: pass self.pair_queue.remove(long_order.id,LONG) self.pair_queue.remove(short_order.id,SHORT) self.logger.info("Success") except ctypes.ArgumentError as e: print("in except: " + str(e)) # self.pair_queue.push(long_order) # self.pair_queue.push(short_order) # converted_long_order = order_conversion(long_order) # converted_short_order = order_conversion(short_order) # # # dll = ctypes.CDLL('./deal/libdeal.so') # # a = ctypes.c_double(self.limit) # b = ctypes.c_double(self.last_price) # # dll.Deal.restype = exchange # result = dll.Deal(converted_long_order, converted_short_order,a,b) # self.save_deal(result,long_order,short_order) # re_order = regenerate_order(result=result) # # self.pair_queue.push(re_order) self.logger.info("Success") return True