<commit_before>/* 
 * Copyright 2012 <NAME> and other contributors
 * http://chirrup.org/
 * 
 * See the file LICENSE for copying permission.
 */

package edu.syr.pcpratts.rootbeer.runtime2.cuda;

import edu.syr.pcpratts.rootbeer.Configuration;
import edu.syr.pcpratts.rootbeer.Constants;
import edu.syr.pcpratts.rootbeer.Main;
import edu.syr.pcpratts.rootbeer.runtime.Serializer;
import edu.syr.pcpratts.rootbeer.runtime.ParallelRuntime;
import edu.syr.pcpratts.rootbeer.runtime.PartiallyCompletedParallelJob;
import edu.syr.pcpratts.rootbeer.runtime.ReadOnlyAnalyzer;
import edu.syr.pcpratts.rootbeer.runtime.RootbeerGpu;
import edu.syr.pcpratts.rootbeer.runtime.Kernel;
import edu.syr.pcpratts.rootbeer.runtime.CompiledKernel;
import edu.syr.pcpratts.rootbeer.runtime.memory.BufferPrinter;
import edu.syr.pcpratts.rootbeer.runtime.memory.Memory;
import edu.syr.pcpratts.rootbeer.runtime.util.Stopwatch;
import edu.syr.pcpratts.rootbeer.util.ResourceReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

public class CudaRuntime2 implements ParallelRuntime {

  private static CudaRuntime2 m_Instance;
  
  public static CudaRuntime2 v(){
    if(m_Instance == null){
      m_Instance = new CudaRuntime2();
    }
    return m_Instance;
  }
  
  private int m_NumBlocks;
  
  private List<Memory> m_ToSpace;
  private List<Memory> m_Texture;
  private Handles m_Handles;
  private Handles m_ExceptionHandles;
  private long m_ToSpaceAddr;
  private long m_GpuToSpaceAddr;
  private long m_TextureAddr;
  private long m_GpuTextureAddr;
  private long m_HandlesAddr;
  private long m_GpuHandlesAddr;
  private long m_ExceptionsHandlesAddr;
  private long m_GpuExceptionsHandlesAddr;
  private long m_ToSpaceSize;
  private int m_NumCores;
  private int m_NumBlocksRun;
  private int m_BlockShape;
  private int m_GridShape;
  private long m_MaxGridDim;
  private long m_NumMultiProcessors;
  
  private List<Kernel> m_JobsToWrite;
  private List<Kernel> m_JobsWritten;
  private List<Kernel> m_NotWritten;
  private List<Long> m_HandlesCache;
  private CompiledKernel m_FirstJob;
  private PartiallyCompletedParallelJob m_Partial;
  
  private List<ToSpaceReader> m_Readers;
  private List<ToSpaceWriter> m_Writers;
  
  private CpuRunner m_CpuRunner;
  private BlockShaper m_BlockShaper;
  
  private CudaRuntime2(){
    Stopwatch watch = new Stopwatch();
    watch.start();    
    CudaLoader loader = new CudaLoader();
    loader.load();
        
    m_BlockShaper = new BlockShaper();
    setup(m_BlockShaper.getMaxBlocksPerProc(), m_BlockShaper.getMaxThreadsPerBlock(), 1300*1024*1024);
    m_JobsToWrite = new ArrayList<Kernel>();
    m_JobsWritten = new ArrayList<Kernel>();  
    m_NotWritten = new ArrayList<Kernel>();
    m_HandlesCache = new ArrayList<Long>();
    m_ToSpace = new ArrayList<Memory>();
    m_Texture = new ArrayList<Memory>();
    m_Readers = new ArrayList<ToSpaceReader>();
    m_Writers = new ArrayList<ToSpaceWriter>();    
    m_NumCores = Runtime.getRuntime().availableProcessors();
    AtomicLong to_space_inst_ptr = new AtomicLong(0);
    AtomicLong to_space_static_ptr = new AtomicLong(0);
    AtomicLong texture_inst_ptr = new AtomicLong(0);
    AtomicLong texture_static_ptr = new AtomicLong(0);
    for(int i = 0; i < m_NumCores; ++i){
      m_ToSpace.add(new FastMemory(m_ToSpaceAddr, to_space_inst_ptr, to_space_static_ptr, m_ToSpaceSize));
      m_Texture.add(new FastMemory(m_TextureAddr, texture_inst_ptr, texture_static_ptr, m_ToSpaceSize));
      m_Readers.add(new ToSpaceReader());
      m_Writers.add(new ToSpaceWriter());
    }
    m_Handles = new Handles(m_HandlesAddr, m_GpuHandlesAddr);
    m_ExceptionHandles = new Handles(m_ExceptionsHandlesAddr, m_GpuExceptionsHandlesAddr);
    m_CpuRunner = new CpuRunner();
    watch.stopAndPrint("CudaRuntime2 ctor: ");
  }
  
  public void memoryTest(){
    MemoryTest test = new MemoryTest();
    test.run(m_ToSpace.get(0));
  }
  <commit_msg>Reduced tospace free memory to 200MB which should hopefully suit most people that are just downloading and running Rootbeer for the first time<commit_after>/* 
 * Copyright 2012 <NAME> and other contributors
 * http://chirrup.org/
 * 
 * See the file LICENSE for copying permission.
 */

package edu.syr.pcpratts.rootbeer.runtime2.cuda;

import edu.syr.pcpratts.rootbeer.Configuration;
import edu.syr.pcpratts.rootbeer.Constants;
import edu.syr.pcpratts.rootbeer.Main;
import edu.syr.pcpratts.rootbeer.runtime.Serializer;
import edu.syr.pcpratts.rootbeer.runtime.ParallelRuntime;
import edu.syr.pcpratts.rootbeer.runtime.PartiallyCompletedParallelJob;
import edu.syr.pcpratts.rootbeer.runtime.ReadOnlyAnalyzer;
import edu.syr.pcpratts.rootbeer.runtime.RootbeerGpu;
import edu.syr.pcpratts.rootbeer.runtime.Kernel;
import edu.syr.pcpratts.rootbeer.runtime.CompiledKernel;
import edu.syr.pcpratts.rootbeer.runtime.memory.BufferPrinter;
import edu.syr.pcpratts.rootbeer.runtime.memory.Memory;
import edu.syr.pcpratts.rootbeer.runtime.util.Stopwatch;
import edu.syr.pcpratts.rootbeer.util.ResourceReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

public class CudaRuntime2 implements ParallelRuntime {

  private static CudaRuntime2 m_Instance;
  
  public static CudaRuntime2 v(){
    if(m_Instance == null){
      m_Instance = new CudaRuntime2();
    }
    return m_Instance;
  }
  
  private int m_NumBlocks;
  
  private List<Memory> m_ToSpace;
  private List<Memory> m_Texture;
  private Handles m_Handles;
  private Handles m_ExceptionHandles;
  private long m_ToSpaceAddr;
  private long m_GpuToSpaceAddr;
  private long m_TextureAddr;
  private long m_GpuTextureAddr;
  private long m_HandlesAddr;
  private long m_GpuHandlesAddr;
  private long m_ExceptionsHandlesAddr;
  private long m_GpuExceptionsHandlesAddr;
  private long m_ToSpaceSize;
  private int m_NumCores;
  private int m_NumBlocksRun;
  private int m_BlockShape;
  private int m_GridShape;
  private long m_MaxGridDim;
  private long m_NumMultiProcessors;
  
  private List<Kernel> m_JobsToWrite;
  private List<Kernel> m_JobsWritten;
  private List<Kernel> m_NotWritten;
  private List<Long> m_HandlesCache;
  private CompiledKernel m_FirstJob;
  private PartiallyCompletedParallelJob m_Partial;
  
  private List<ToSpaceReader> m_Readers;
  private List<ToSpaceWriter> m_Writers;
  
  private CpuRunner m_CpuRunner;
  private BlockShaper m_BlockShaper;
  
  private CudaRuntime2(){
    Stopwatch watch = new Stopwatch();
    watch.start();    
    CudaLoader loader = new CudaLoader();
    loader.load();
        
    m_BlockShaper = new BlockShaper();
    // the 200*1024*1024 factor here is the amount of free memory that should NOT
    // be allocated for transfering data to and from the gpu
    setup(m_BlockShaper.getMaxBlocksPerProc(), m_BlockShaper.getMaxThreadsPerBlock(), 200*1024*1024);
    m_JobsToWrite = new ArrayList<Kernel>();
    m_JobsWritten = new ArrayList<Kernel>();  
    m_NotWritten = new ArrayList<Kernel>();
    m_HandlesCache = new ArrayList<Long>();
    m_ToSpace = new ArrayList<Memory>();
    m_Texture = new ArrayList<Memory>();
    m_Readers = new ArrayList<ToSpaceReader>();
    m_Writers = new ArrayList<ToSpaceWriter>();    
    m_NumCores = Runtime.getRuntime().availableProcessors();
    AtomicLong to_space_inst_ptr = new AtomicLong(0);
    AtomicLong to_space_static_ptr = new AtomicLong(0);
    AtomicLong texture_inst_ptr = new AtomicLong(0);
    AtomicLong texture_static_ptr = new AtomicLong(0);
    for(int i = 0; i < m_NumCores; ++i){
      m_ToSpace.add(new FastMemory(m_ToSpaceAddr, to_space_inst_ptr, to_space_static_ptr, m_ToSpaceSize));
      m_Texture.add(new FastMemory(m_TextureAddr, texture_inst_ptr, texture_static_ptr, m_ToSpaceSize));
      m_Readers.add(new ToSpaceReader());
      m_Writers.add(new ToSpaceWriter());
    }
    m_Handles = new Handles(m_HandlesAddr, m_GpuHandlesAddr);
    m_ExceptionHandles = new Handles(m_ExceptionsHandlesAddr, m_GpuExceptionsHandlesAddr);
    m_CpuRunner = new CpuRunner();
    watch.stopAndPrint("CudaRuntime2 ctor: ");
  }
  
  public void memoryTest(){
    MemoryTest test = new MemoryTest();
    test.run(m_ToSpace.get(0));
  }
  
{'fraction_non_alphanumeric': 0.0901313411254368, 'fraction_numerical': 0.008555247620195204, 'mean_word_length': 4.804195804195804, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5934947', 'n_tokens_mistral': 2859, 'n_tokens_neox': 2800, 'n_words': 573}