コード例 #1
0
def genMCHistogramsOpenCL(distribution,
                          hist_range,
                          distribution_params=[],
                          iterations=1000,
                          numBins=1000):
    tester = clsim.I3CLSimRandomDistributionTester(
        device=openCLDevice,
        workgroupSize=workgroupSize,
        workItemsPerIteration=workItemsPerIteration,
        randomService=rng,
        randomDistribution=distribution,
        runtimeParameters=distribution_params)

    values = tester.GenerateRandomNumbers(iterations)
    samples = len(values)
    print("generated")

    range_width = hist_range[1] - hist_range[0]

    num_orig, bins = scipy.histogram(values, range=hist_range, bins=numBins)
    print("hist1 complete")

    del values  # not needed anymore
    print("deleted")

    num = []
    for number in num_orig:
        num.append(
            float(number) / float(samples) /
            float(range_width / float(numBins)))
    num = numpy.array(num)

    bins = numpy.array(bins[:-1]) + (bins[1] - bins[0]) / 2.

    return dict(num=num, bins=bins)
コード例 #2
0
def genMCHistogramsOpenCL(distribution, rng, iterations=100, numBins=1000):
    # get OpenCL CPU devices
    openCLDevices = [
        device for device in clsim.I3CLSimOpenCLDevice.GetAllDevices()
        if device.cpu
    ]
    if len(openCLDevices) == 0:
        raise RuntimeError("No CPU OpenCL devices available!")
    openCLDevice = openCLDevices[0]

    openCLDevice.useNativeMath = False
    workgroupSize = 1
    workItemsPerIteration = 10240
    print("           using platform:", openCLDevice.platform)
    print("             using device:", openCLDevice.device)
    print("            workgroupSize:", workgroupSize)
    print("    workItemsPerIteration:", workItemsPerIteration)

    tester = clsim.I3CLSimRandomDistributionTester(
        device=openCLDevice,
        workgroupSize=workgroupSize,
        workItemsPerIteration=workItemsPerIteration,
        randomService=rng,
        randomDistribution=distribution)

    print("maxWorkgroupSizeForKernel:", tester.maxWorkgroupSize)

    angles = tester.GenerateRandomNumbers(iterations)
    samples = len(angles)

    print("generated")

    angles = numpy.array(angles)  # convert to numpy array
    print("converted")

    numAng_orig, binsAng = scipy.histogram(numpy.arccos(angles) *
                                           (180. / math.pi),
                                           range=(0., 180.),
                                           bins=numBins)
    print("hist1 complete")

    numCos_orig, binsCos = scipy.histogram(angles,
                                           range=(-1., 1.),
                                           bins=numBins)
    print("hist2 complete")

    del angles  # not needed anymore
    print("deleted")

    numAng = []
    for i, number in enumerate(numAng_orig):
        binWidth = math.cos(binsAng[i] * math.pi / 180.) - math.cos(
            binsAng[i + 1] * math.pi / 180.)
        numAng.append(float(number) / float(samples) / binWidth)
    numAng = numpy.array(numAng)

    numCos = []
    for i, number in enumerate(numCos_orig):
        numCos.append(
            float(number) / float(samples) / float(2. / float(numBins)))
    numCos = numpy.array(numCos)

    binsAng = numpy.array(binsAng[:-1]) + (binsAng[1] - binsAng[0]) / 2.
    binsCos = numpy.array(binsCos[:-1]) + (binsCos[1] - binsCos[0]) / 2.

    return dict(cos=dict(num=numCos, bins=binsCos),
                ang=dict(num=numAng, bins=binsAng))