<commit_before>import array

import corepy.arch.spu.isa as spu
from corepy.arch.spu.types.spu_types import SignedWord
from corepy.arch.spu.lib.iterators import memory_desc, spu_vec_iter, \
     stream_buffer, syn_iter, parallel
from corepy.arch.spu.platform import InstructionStream, ParallelInstructionStream, \
     Processor, aligned_memory
  
def SimpleSPU():
  """
  A very simple SPU that computes 11 + 31 and returns 0xA on success.
  """
  code = InstructionStream()
  proc = Processor()

  spu.set_active_code(code)
  

  # Acquire two registers
  x    = code.acquire_register()
  test = code.acquire_register()

  spu.xor(x, x, x) # zero x
  spu.ai(x, x, 11) # x = x + 11
  spu.ai(x, x, 31) # x = x + 31

  spu.ceqi(test, x, 42) # test = (x == 42)

  # If test is false (all 0s), skip the stop(0x200A) instruction
  spu.brz(test, 2)
  spu.stop(0x200A)
  spu.stop(0x200B)
  
  r = proc.execute(code) 
  assert(r == 0xA)

  return


def MemoryDescExample():
  """
  This example uses a memory descriptor to move 20k integers back and 
  forth between main memory and the SPU local store. Each value is
  incremented by 1 while on the SPU.
  
  Memory descriptors are a general purpose method for describing a
  region of memory.  Memory is described by a typecode, address, and
  size.  Memory descriptors can be initialized by hand or from an
  array or buffer object.

  For main memory, memory descriptors are useful for transfering data
  between main memory and an SPU's local store.  The get/put methods
  on a memory descriptor generate the SPU code to move data of any
  size between main memory and local store.

  Memory descriptors can also be used with spu_vec_iters to describe
  the region of memory to iterate over.  The typecode in the memory
  descriptor is used to determine the type for the loop induction
  variable.

  Note that there is currently no difference between memory
  descriptors for main memory and local store.  It's up to the user to
  make sure the memory descriptor settings make sense in the current
  context.  (this will probably change in the near future)

  Note: get/put currently use loops rather than display lists for
        transferring data over 16k.
  """
  
  code = InstructionStream()
  proc = Processor()

  spu.set_active_code(code)

  data_size = 20000

  # Create a python array
  data = array.array('I', range(data_size))

  # Align the data in the array
  a_data = aligned_memory(len(data), typecode = 'I')
  a_data.copy_to(data.buffer_info()[0], len(data))
  
  # Create memory descriptor for the data in main memory
  data_desc = memory_desc('I')
  data_desc.from_array(a_data)

  # Transfer the data to 0x0 in the local store
  data_desc.get(code, 0)

  # Create memory descriptor for the data in the local store for use
  # in the iterator  
  lsa_data = memory_desc('I', 0, data_size)

  # Add one to each value
  for x in spu_vec_iter(code, lsa_data):
    x.v = x + 1

  # Transfer the data back to main memory
  data_desc.put(code, 0)

  # Execute the synthetic program
  r = proc.execute(code) # , debug = True)

  # Copy it back to the Python array
  a_data.copy_from(data.buffer_info()[0], len(data))

  for i in range(data_size):
    assert(data[i] == i + 1)
  <commit_msg>checkin for DongInn to test commits...<commit_after>import array

import corepy.arch.spu.isa as spu
from corepy.arch.spu.types.spu_types import SignedWord
from corepy.arch.spu.lib.iterators import memory_desc, spu_vec_iter, \
     stream_buffer, syn_iter, parallel
from corepy.arch.spu.platform import InstructionStream, ParallelInstructionStream, \
     Processor, aligned_memory
  
def SimpleSPU():
  """
  A very simple SPU that computes 11 + 31 and returns 0xA on success.
  """
  code = InstructionStream()
  proc = Processor()

  spu.set_active_code(code)
  

  # Acquire two registers
  x    = code.acquire_register()
  test = code.acquire_register()

  spu.xor(x, x, x) # zero x
  spu.ai(x, x, 11) # x = x + 11
  spu.ai(x, x, 31) # x = x + 31

  spu.ceqi(test, x, 42) # test = (x == 42)

  # If test is false (all 0s), skip the stop(0x200A) instruction
  spu.brz(test, 2)
  spu.stop(0x200A)
  spu.stop(0x200B)
  
  r = proc.execute(code) 
  assert(r == 0xA)

  return


def MemoryDescExample():
  """
  This example uses a memory descriptor to move 20k integers back and 
  forth between main memory and the SPU local store. Each value is
  incremented by 1 while on the SPU.
  
  Memory descriptors are a general purpose method for describing a
  region of memory.  Memory is described by a typecode, address, and
  size.  Memory descriptors can be initialized by hand or from an
  array or buffer object.

  For main memory, memory descriptors are useful for transfering data
  between main memory and an SPU's local store.  The get/put methods
  on a memory descriptor generate the SPU code to move data of any
  size between main memory and local store.

  Memory descriptors can also be used with spu_vec_iters to describe
  the region of memory to iterate over.  The typecode in the memory
  descriptor is used to determine the type for the loop induction
  variable.

  Note that there is currently no difference between memory
  descriptors for main memory and local store.  It's up to the user to
  make sure the memory descriptor settings make sense in the current
  context.  (this will probably change in the near future)

  Note: get/put currently use loops rather than display lists for
        transferring data over 16k.
  """
  
  code = InstructionStream()
  proc = Processor()

  code.debug = True
  spu.set_active_code(code)

  data_size = 20000

  # Create a python array
  data = array.array('I', range(data_size))

  # Align the data in the array
  a_data = aligned_memory(len(data), typecode = 'I')
  a_data.copy_to(data.buffer_info()[0], len(data))
  
  # Create memory descriptor for the data in main memory
  data_desc = memory_desc('I')
  data_desc.from_array(a_data)

  # Transfer the data to 0x0 in the local store
  data_desc.get(code, 0)

  # Create memory descriptor for the data in the local store for use
  # in the iterator  
  lsa_data = memory_desc('i', 0, data_size)

  # Add one to each value
  for x in spu_vec_iter(code, lsa_data):
    x.v = x + 1

  test = code.acquire_register()
  spu.lqa(test, 0)
  spu.ceqi(test, test, 1) # test = (x == 1)

  spu.brnz(test, 2) # if test == 0:
  spu.stop(0xA)     #   stop
  
  # Transfer the data back to main memory
  data_desc.put(code, 0)

  # Execute the synthetic program
  r = proc.execute(code) # , debug = True)
  code.print_code()

  # Copy it back to the Python array
  a_data.copy_from(data.buffer_info()[0], len(data))

  print data[:20]

  for i in range(data_size):
    assert(data[i] == i + i)
  
{'fraction_non_alphanumeric': 0.08058065471781958, 'fraction_numerical': 0.016145756184268997, 'mean_word_length': 3.3986970684039086, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '199373', 'n_tokens_mistral': 2330, 'n_tokens_neox': 2160, 'n_words': 843}