Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. import cudf
  2. import cugraph
  3. from numba import cuda
  4. from numba.cuda.random import create_xoroshiro128p_states, xoroshiro128p_uniform_float32
  5. import numpy as np
  6.  
  7. # gdf = cudf.read_csv('zachary.ssv', header=None, sep=' ', dtype=['int32', 'int32'])
  8. gdf = cudf.read_csv('Flickr-labelled.edgelist', header=None, sep=' ', dtype=['int32', 'int32'])
  9. gdf.columns = ['src', 'dest']
  10. gdf = gdf.sort_values(by='src')
  11. G = cugraph.Graph()
  12. G.add_edge_list(gdf['src'], gdf['dest'])
  13. adj_list = G.view_adj_list()
  14. offsets, indices = adj_list[0], adj_list[1]
  15. offsets, indices = cuda.to_device(offsets), cuda.to_device(indices)
  16. nodes = gdf['src'].unique().values
  17.  
  18.  
  19. @cuda.jit
  20. def generate_walks(start_nodes, out, offsets, indices, rng_states):
  21. thread_id = cuda.grid(1)
  22. if thread_id < start_nodes.size: # Check array boundaries
  23. start_node = start_nodes[thread_id]
  24. out[thread_id][0] = start_node
  25. curr_node = start_node
  26. for i in range(1, out.shape[1]):
  27. # get neighbors
  28. if curr_node == -1:
  29. next_node = curr_node
  30. else:
  31. start_idx = offsets[curr_node]
  32. end_idx = offsets[curr_node + 1]
  33. neighbors = indices[start_idx:end_idx]
  34. num_neighbors = len(neighbors)
  35. if num_neighbors > 0:
  36. rand_float = xoroshiro128p_uniform_float32(rng_states, thread_id)
  37. choice = int(rand_float * num_neighbors)
  38. next_node = neighbors[choice]
  39. else:
  40. next_node = -1
  41. out[thread_id][i] = next_node
  42. curr_node = next_node
  43.  
  44.  
  45. walk_length = 80
  46. walks_per_node = 1
  47. start_nodes = np.hstack([nodes] * walks_per_node)
  48. out = np.full_like(start_nodes, -2).repeat(walk_length).reshape(start_nodes.shape[0], walk_length)
  49. print(out.nbytes / 1e9)
  50. threads_per_block = 64
  51. blocks_per_grid = (start_nodes.size + (threads_per_block - 1)) // threads_per_block
  52. rng_states = create_xoroshiro128p_states(threads_per_block * blocks_per_grid, seed=1)
  53. generate_walks[blocks_per_grid, threads_per_block](start_nodes, out, offsets, indices, rng_states)
  54.  
  55. # walks are stored in "out", with -1 denoting end of walk
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement