In [3]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [1]:
cd ..


/home/chiroptera/workspace/QCThesis

In [240]:
import MyML.graph.mst
reload(MyML.graph.mst)
from MyML.graph.mst import boruvkaMinhoSeq, boruvkaMinhoSeqForest

In [249]:
dest2 = np.array([3, 1, 2, 3, 0, 2, 0, 1, 0, 1, 7, 5, 6, 4, 6, 5, 4, 4], dtype = np.int32)
weight2 = np.array([4, 2, 5, 3, 2, 4, 5, 4, 4, 3, 2, 4, 8, 4, 3, 3, 8, 2], dtype = np.float32)
fe2 = np.array([0, 3, 6, 8, 10, 13, 15, 17], dtype = np.int32)
od2 = np.array([3, 3, 2, 2, 3, 2, 2, 1], dtype = np.int32)

In [250]:
mst_solver = boruvkaMinhoSeqForest(dest2, weight2, fe2, od2)

In [243]:
mst_solver.fit()


Out[243]:
array([ 4,  7,  9, 15, 17, 13,  3], dtype=int32)

In [190]:
mst_solver = boruvkaMinhoSeq(dest2, weight2, fe2, od2)

In [261]:
print mst_solver.n_components
print mst_solver.n_mst


3
2

In [262]:
#%%debug
mst_solver.findMinEdge()
mst_solver.vertex_minedge


Out[262]:
array([-1,  0,  2], dtype=int32)

In [263]:
mst_solver.removeMirrored()
mst_solver.vertex_minedge


Out[263]:
array([-1, -1,  2], dtype=int32)

In [265]:
mst_solver.initColors()
print mst_solver.colors


[0 1 1]

In [266]:
while(not mst_solver.converged):
    mst_solver.propagateColors()
print mst_solver.colors


[0 1 1]

In [267]:
mst_solver.createNewVertexID()
print mst_solver.new_vertex


[0 1 2]

In [268]:
mst_solver.countNewVertex()
print mst_solver.new_first_edge
print mst_solver.new_outDegree


[0 0]
[0 0]

In [269]:
mst_solver.removeMST()

In [271]:
mst_solver.n_mst


Out[271]:
3

In [276]:
print mst_solver.edge_id
print mst_solver.dest
print mst_solver.weight
print mst_solver.firstEdge
print mst_solver.outDegree


[]
[]
[]
[0 0]
[0 0]

In [273]:
mst_solver.assignInsert()
print mst_solver.edge_id
print mst_solver.dest
print mst_solver.weight
print mst_solver.firstEdge
print mst_solver.outDegree


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-273-7d152a8cd8ba> in <module>()
----> 1 mst_solver.assignInsert()
      2 print mst_solver.edge_id
      3 print mst_solver.dest
      4 print mst_solver.weight
      5 print mst_solver.firstEdge

/home/chiroptera/workspace/QCThesis/MyML/graph/mst.py in assignInsert(self)
    850     def assignInsert(self):
    851 
--> 852         top_edge = self.new_first_edge.copy() # pointer to new destination indices
    853         next_num_edges = self.new_outDegree.sum() # number of edges in new contracted graph
    854         new_dest = np.empty(next_num_edges, dtype = np.int32) # new destination array

AttributeError: boruvkaMinhoSeqForest instance has no attribute 'new_first_edge'

In [275]:
mst_solver.n_components


Out[275]:
2

In [ ]:


In [ ]:
mst_solver.fit()

In [1]:
import numpy as np
from numba import cuda, int32

In [8]:
a = np.arange(2**4, dtype=np.int32)
dA = cuda.to_device(a)

In [1]:
import numpy as np
import numba
from numba import cuda
@cuda.jit
def smallScanSM(x):
    temp = cuda.shared.array(0,dtype=numba.int32)
    thid = cuda.grid(1)
    #thid = cuda.threadIdx.x

    # copy a to shared memory with a shift right
    if thid > 0:
        temp[thid] = x[thid-1]
    else:
        temp[thid] = 0
        
    cuda.syncthreads()
    
    d = 1
    while(d < x.size):
        if thid >= d:
            val_a = temp[thid]
            val_b = temp[thid - d]
            temp[thid] = val_a + val_b
        d *= 2
        cuda.syncthreads()
    
    # copy results back    
    cuda.syncthreads()
    x[thid] = temp[thid]
    
a = np.arange(2**4, dtype=np.int32)

stream = 0
block_count = 1
thread_count = a.size
dyn_shared_size = a.size * a.itemsize
dA = cuda.to_device(a)
smallScanSM[block_count,thread_count,stream,dyn_shared_size](dA)
a = dA.copy_to_host()

In [2]:
a = dA.copy_to_host()

In [3]:
a


Out[3]:
array([  0,   0,   1,   3,   6,  10,  15,  21,  28,  36,  45,  55,  66,
        78,  91, 105], dtype=int32)

In [1]:
cd ~/QCThesis


/home/chiroptera/workspace/QCThesis

In [2]:
import numpy as np
from MyML.cluster.K_Means3 import K_Means, _cu_label_kernel_dists
from MyML.cluster.eac import EAC
from MyML.metrics import accuracy
from MyML.helper.partition import generateEnsemble
from sklearn.datasets.samples_generator import make_blobs
from sklearn.cluster import KMeans
from numbapro import cuda

In [8]:
n_samples = 1e4
n_features = 2
centers = 6
n_samples = np.int(n_samples)
data, gt = make_blobs(n_samples = n_samples, n_features = n_features, centers = centers)
data = data.astype(np.float32)
n_samples_sqrt = np.int(np.sqrt(n_samples))
n_clusters = [n_samples_sqrt / 2, n_samples_sqrt]
n_clusters = map(int,n_clusters)

In [9]:
generator = K_Means(cuda_mem="manual")

In [10]:
generator.n_clusters = n_clusters[-1]
generator.max_iter = 1

In [11]:
generator.N = n_samples
generator.D = n_features

generator.centroids = generator._init_centroids(data)
generator.labels = cuda.pinned_array(shape=generator.N, dtype = np.int32)
generator._dists = cuda.pinned_array(shape=generator.N, dtype = np.float32)

generator._compute_cuda_dims(data)
gridDim = generator._gridDim
blockDim = generator._blockDim

dData = cuda.to_device(data)

dCentroids = cuda.to_device(generator.centroids)

dLabels = cuda.device_array_like(generator.labels)
dDists = cuda.device_array_like(generator._dists)


---------------------------------------------------------------------------
CudaAPIError                              Traceback (most recent call last)
<ipython-input-11-6aceeccaf898> in <module>()
      3 
      4 generator.centroids = generator._init_centroids(data)
----> 5 generator.labels = cuda.pinned_array(shape=generator.N, dtype = np.int32)
      6 generator._dists = cuda.pinned_array(shape=generator.N, dtype = np.float32)
      7 

/home/chiroptera/anaconda/lib/python2.7/site-packages/numba/cuda/cudadrv/devices.pyc in _require_cuda_context(*args, **kws)
    255     def _require_cuda_context(*args, **kws):
    256         get_context()
--> 257         return fn(*args, **kws)
    258 
    259     return _require_cuda_context

/home/chiroptera/anaconda/lib/python2.7/site-packages/numba/cuda/api.pyc in pinned_array(shape, dtype, strides, order)
     83     bytesize = driver.memory_size_from_info(shape, strides,
     84                                             dtype.itemsize)
---> 85     buffer = current_context().memhostalloc(bytesize)
     86     return np.ndarray(shape=shape, strides=strides, dtype=dtype, order=order,
     87                       buffer=buffer)

/home/chiroptera/anaconda/lib/python2.7/site-packages/numba/cuda/cudadrv/driver.pyc in memhostalloc(self, bytesize, mapped, portable, wc)
    511 
    512     def memhostalloc(self, bytesize, mapped=False, portable=False, wc=False):
--> 513         self.trashing.service()
    514 
    515         pointer = c_void_p()

/home/chiroptera/anaconda/lib/python2.7/site-packages/numba/servicelib/service.pyc in service(self)
     28                 # Prevent recursion
     29                 self.enabled = False
---> 30                 next(self._task)
     31             finally:
     32                 self.enabled = enable

/home/chiroptera/anaconda/lib/python2.7/site-packages/numba/cuda/cudadrv/driver.pyc in process(self, _arg)
    303                 cb = self.trash.pop()
    304                 # Invoke callback
--> 305                 cb()
    306                 count += 1
    307             yield

/home/chiroptera/anaconda/lib/python2.7/site-packages/numba/cuda/cudadrv/driver.pyc in <lambda>()
    686 def _pinnedalloc_finalizer(trashing, handle):
    687     def core():
--> 688         trashing.add_trash(lambda: driver.cuMemFreeHost(handle))
    689 
    690     return core

/home/chiroptera/anaconda/lib/python2.7/site-packages/numba/cuda/cudadrv/driver.pyc in safe_cuda_api_call(*args)
    214         def safe_cuda_api_call(*args):
    215             retcode = libfn(*args)
--> 216             self._check_error(fname, retcode)
    217 
    218         setattr(self, fname, safe_cuda_api_call)

/home/chiroptera/anaconda/lib/python2.7/site-packages/numba/cuda/cudadrv/driver.pyc in _check_error(self, fname, retcode)
    244             errname = ERROR_MAP.get(retcode, "UNKNOWN_CUDA_ERROR")
    245             msg = "Call to %s results in %s" % (fname, errname)
--> 246             raise CudaAPIError(retcode, msg)
    247 
    248     def get_device(self, devnum=0):

CudaAPIError: Call to cuMemFreeHost results in CUDA_ERROR_LAUNCH_TIMEOUT

In [7]:
startE = cuda.event()
endE = cuda.event()

startE.record()
_cu_label_kernel_dists[gridDim,blockDim](dData,dCentroids,dLabels,dDists)
endE.record()
endE.synchronize()
print cuda.event_elapsed_time(startE,endE)

startE.record()
dDists.copy_to_host(ary = generator._dists)
labels = dLabels.copy_to_host(ary = generator.labels)
endE.record()
endE.synchronize()
print cuda.event_elapsed_time(startE,endE)


---------------------------------------------------------------------------
CudaAPIError                              Traceback (most recent call last)
<ipython-input-7-f3a0df059ea2> in <module>()
      5 _cu_label_kernel_dists[gridDim,blockDim](dData,dCentroids,dLabels,dDists)
      6 endE.record()
----> 7 endE.synchronize()
      8 print cuda.event_elapsed_time(startE,endE)
      9 

/home/chiroptera/anaconda/lib/python2.7/site-packages/numba/cuda/cudadrv/driver.pyc in synchronize(self)
    952         Synchronize the host thread for the completion of the event.
    953         """
--> 954         driver.cuEventSynchronize(self.handle)
    955 
    956     def wait(self, stream=0):

/home/chiroptera/anaconda/lib/python2.7/site-packages/numba/cuda/cudadrv/driver.pyc in safe_cuda_api_call(*args)
    214         def safe_cuda_api_call(*args):
    215             retcode = libfn(*args)
--> 216             self._check_error(fname, retcode)
    217 
    218         setattr(self, fname, safe_cuda_api_call)

/home/chiroptera/anaconda/lib/python2.7/site-packages/numba/cuda/cudadrv/driver.pyc in _check_error(self, fname, retcode)
    244             errname = ERROR_MAP.get(retcode, "UNKNOWN_CUDA_ERROR")
    245             msg = "Call to %s results in %s" % (fname, errname)
--> 246             raise CudaAPIError(retcode, msg)
    247 
    248     def get_device(self, devnum=0):

CudaAPIError: Call to cuEventSynchronize results in CUDA_ERROR_LAUNCH_TIMEOUT

In [8]:
%time generator.fit(data)


---------------------------------------------------------------------------
CudaAPIError                              Traceback (most recent call last)
<ipython-input-8-ce4d6e0bd497> in <module>()
----> 1 get_ipython().magic(u'time generator.fit(data)')

/home/chiroptera/anaconda/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in magic(self, arg_s)
   2305         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2306         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2307         return self.run_line_magic(magic_name, magic_arg_s)
   2308 
   2309     #-------------------------------------------------------------------------

/home/chiroptera/anaconda/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in run_line_magic(self, magic_name, line)
   2226                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2227             with self.builtin_trap:
-> 2228                 result = fn(*args,**kwargs)
   2229             return result
   2230 

/home/chiroptera/anaconda/lib/python2.7/site-packages/IPython/core/magics/execution.pyc in time(self, line, cell, local_ns)

/home/chiroptera/anaconda/lib/python2.7/site-packages/IPython/core/magic.pyc in <lambda>(f, *a, **k)
    191     # but it's overkill for just that one bit of state.
    192     def magic_deco(arg):
--> 193         call = lambda f, *a, **k: f(*a, **k)
    194 
    195         if callable(arg):

/home/chiroptera/anaconda/lib/python2.7/site-packages/IPython/core/magics/execution.pyc in time(self, line, cell, local_ns)
   1160         if mode=='eval':
   1161             st = clock2()
-> 1162             out = eval(code, glob, local_ns)
   1163             end = clock2()
   1164         else:

<timed eval> in <module>()

/home/chiroptera/workspace/QCThesis/MyML/cluster/K_Means3.pyc in fit(self, data)
    107         while not stopcond:
    108             # compute labels
--> 109             labels = self._label(data,self.centroids)
    110 
    111             self.iters_ += 1 #increment iteration counter

/home/chiroptera/workspace/QCThesis/MyML/cluster/K_Means3.pyc in _label(self, data, centroids)
    178         # we need array for distances to check convergence
    179         if self._mode == "cuda":
--> 180             labels = self._cu_label(data, centroids)
    181         elif self._mode == "special": #for tests only
    182             labels=np.empty(self.N, dtype=np.int32)

/home/chiroptera/workspace/QCThesis/MyML/cluster/K_Means3.pyc in _cu_label(self, data, centroids)
    426 
    427             # copy labels from device to host
--> 428             dLabels.copy_to_host(ary = labels)
    429             # copy distance to centroids from device to host
    430             dists = dDists.copy_to_host()

/home/chiroptera/anaconda/lib/python2.7/site-packages/numba/cuda/cudadrv/devicearray.pyc in copy_to_host(self, ary, stream)
    193         assert self.alloc_size >= 0, "Negative memory size"
    194         if self.alloc_size != 0:
--> 195             _driver.device_to_host(hostary, self, self.alloc_size, stream=stream)
    196 
    197         if ary is None:

/home/chiroptera/anaconda/lib/python2.7/site-packages/numba/cuda/cudadrv/driver.pyc in device_to_host(dst, src, size, stream)
   1377         fn = driver.cuMemcpyDtoH
   1378 
-> 1379     fn(host_pointer(dst), device_pointer(src), size, *varargs)
   1380 
   1381 

/home/chiroptera/anaconda/lib/python2.7/site-packages/numba/cuda/cudadrv/driver.pyc in safe_cuda_api_call(*args)
    214         def safe_cuda_api_call(*args):
    215             retcode = libfn(*args)
--> 216             self._check_error(fname, retcode)
    217 
    218         setattr(self, fname, safe_cuda_api_call)

/home/chiroptera/anaconda/lib/python2.7/site-packages/numba/cuda/cudadrv/driver.pyc in _check_error(self, fname, retcode)
    244             errname = ERROR_MAP.get(retcode, "UNKNOWN_CUDA_ERROR")
    245             msg = "Call to %s results in %s" % (fname, errname)
--> 246             raise CudaAPIError(retcode, msg)
    247 
    248     def get_device(self, devnum=0):

CudaAPIError: Call to cuMemcpyDtoH results in CUDA_ERROR_LAUNCH_TIMEOUT

In [21]:
del generator


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-21-48cb156c48fc> in <module>()
----> 1 del generator

NameError: name 'generator' is not defined

In [6]:
from numbapro import cuda

In [21]:
a=cuda.to_device(data)

In [24]:
a=cuda.device_array(10000)

In [28]:
10000/1000


Out[28]:
10

In [25]:
b=a.split(1000)

In [26]:
c=b.next()

In [27]:
b.


Out[27]:
8000

In [ ]: