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

package edu.syr.pcpratts.rootbeer.runtime;

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.testcases.rootbeertest.kerneltemplate.MatrixKernel;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;

public abstract class Serializer {

  public Memory mMem;
  public Memory mTextureMem;

  private static final Map<Object, Long> mWriteToGpuCache;
  private static final Map<Long, Object> mReverseWriteToGpuCache;
  private static final Map<Long, Object> mReadFromGpuCache;
  private static Map<Long, Integer> m_classRefToTypeNumber;
  
  private ReadOnlyAnalyzer m_Analyzer;
  
  static {
    mWriteToGpuCache = new IdentityHashMap<Object, Long>();
    mReverseWriteToGpuCache = new HashMap<Long, Object>();
    mReadFromGpuCache = new HashMap<Long, Object>();
    m_classRefToTypeNumber = new HashMap<Long, Integer>();
  }
  
  public Serializer(Memory mem, Memory texture_mem){
    mMem = mem;
    mTextureMem = texture_mem;
    mReadFromGpuCache.clear();
    mWriteToGpuCache.clear();
    mReverseWriteToGpuCache.clear();
    m_classRefToTypeNumber.clear();
  }
  
  public void setAnalyzer(ReadOnlyAnalyzer analyzer){
    m_Analyzer = analyzer;
  }

  public void writeStaticsToHeap(){
    doWriteStaticsToHeap();
  }

  public long writeToHeap(Object o){
    return writeToHeap(o, true);
  }
  
  private static class WriteCacheResult {
    public long m_Ref;
    public boolean m_NeedToWrite;
    public WriteCacheResult(long ref, boolean need_to_write){
      m_Ref = ref;
      m_NeedToWrite = need_to_write;
    }
  }
  
  public void addClassRef(long ref, int class_number){
    m_classRefToTypeNumber.put(ref, class_number);
  }
  
  public int[] getClassRefArray(){
    int max_type = 0;
    for(int num : m_classRefToTypeNumber.values()){
      if(num > max_type){
        max_type = num;
      }
    }
    int[] ret = new int[max_type+1];
    for(long value : m_classRefToTypeNumber.keySet()){
      int pos = m_classRefToTypeNumber.get(value);
      ret[pos] = (int) (value >> 4);
    }
    return ret;
  }
  
  private static synchronized WriteCacheResult checkWriteCache(Object o, int size, boolean read_only, Memory mem){
    //strings are cached in Java 1.6, we need to make strings individual units
    //for rootbeer so concurrent modifications change different objects
    if(o instanceof String){
      long ref = mem.mallocWithSize(size);
      return new WriteCacheResult(ref, true);
    } else {
      if(mWriteToGpuCache.containsKey(o)){
        long ref = mWriteToGpuCache.get(o);
        return new WriteCacheResult(ref, false);
      }
      long ref = mem.mallocWithSize(size);
      mWriteToGpuCache.put(o, ref);
      mReverseWriteToGpuCache.put(ref, o);
      return new WriteCacheResult(ref, true);
    }
  }
  
  public Object writeCacheFetch(long ref){
    synchronized(mWriteToGpuCache){
      if(mReverseWriteToGpuCache.containsKey(ref)){
        return mReverseWriteToGpuCache.get(ref);
      }
      return null;
    }
  }
  
  public long writeToHeap(Object o, boolean write_data){
    if(o == null)
      return -1;    
    int size = doGetSize(o);
    boolean read_only = false;
    WriteCacheResult result;
    result = checkWriteCache(o, size, read_only, mMem);
    
    if(result.m_NeedToWrite == false){
      return result.m_Ref;
    }
    if(o == null){
      System.out.println("writeToHeap: null at addr: "+result.m_Ref);
    } else {
      System.out.println("writeToHeap: "+o.toString()+" at addr: "+result.m_Ref);
    }
    doWriteToHeap(o, write_data, result.m_Ref, read_only);
    return result.m_Ref;
  }
  
  protected Object checkCache(long address, Object item){
    synchronized(mReadFromGpuCache){
      if(mReadFromGpuCache.containsKey(address)){
        return mReadFromGpuCache.get(address);
      } else {
        mReadFromGpuCache.put(address, item);
        return item;
      }
    }
  }

  public Object readFromHeap(Object o, boolean read_data, long address){
    synchronized(mReadFromGpuCache){
      if(mReadFromGpuCache.containsKey(address)){
        Object ret = mReadFromGpuCache.get(address);  
        return ret;
      }
    }
    long null_ptr_check = address >> 4;
    if(null_ptr_check == -1){
      return null;
    }
    if(o == null){
      System.out.println("readFromHeap: null. addr: "+address);
    } else {
      System.out.println("readFromHeap: "+o.toString()+". addr: "+address);
    }
    BufferPrinter printer = new BufferPrinter();
    printer.print(mMem, address, 128);
    
    Object ret = doReadFromHeap(o, read_data, address);
    return checkCache(address, ret);
  }

  public void readStaticsFromHeap(){
    doReadStaticsFromHeap();
  }
 
  public Object readField(Object base, String name){<commit_msg>cleaning up printlns<commit_after>/* 
 * Copyright 2012 <NAME> and other contributors
 * http://chirrup.org/
 * 
 * See the file LICENSE for copying permission.
 */

package edu.syr.pcpratts.rootbeer.runtime;

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.testcases.rootbeertest.kerneltemplate.MatrixKernel;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;

public abstract class Serializer {

  public Memory mMem;
  public Memory mTextureMem;

  private static final Map<Object, Long> mWriteToGpuCache;
  private static final Map<Long, Object> mReverseWriteToGpuCache;
  private static final Map<Long, Object> mReadFromGpuCache;
  private static Map<Long, Integer> m_classRefToTypeNumber;
  
  private ReadOnlyAnalyzer m_Analyzer;
  
  static {
    mWriteToGpuCache = new IdentityHashMap<Object, Long>();
    mReverseWriteToGpuCache = new HashMap<Long, Object>();
    mReadFromGpuCache = new HashMap<Long, Object>();
    m_classRefToTypeNumber = new HashMap<Long, Integer>();
  }
  
  public Serializer(Memory mem, Memory texture_mem){
    mMem = mem;
    mTextureMem = texture_mem;
    mReadFromGpuCache.clear();
    mWriteToGpuCache.clear();
    mReverseWriteToGpuCache.clear();
    m_classRefToTypeNumber.clear();
  }
  
  public void setAnalyzer(ReadOnlyAnalyzer analyzer){
    m_Analyzer = analyzer;
  }

  public void writeStaticsToHeap(){
    doWriteStaticsToHeap();
  }

  public long writeToHeap(Object o){
    return writeToHeap(o, true);
  }
  
  private static class WriteCacheResult {
    public long m_Ref;
    public boolean m_NeedToWrite;
    public WriteCacheResult(long ref, boolean need_to_write){
      m_Ref = ref;
      m_NeedToWrite = need_to_write;
    }
  }
  
  public void addClassRef(long ref, int class_number){
    m_classRefToTypeNumber.put(ref, class_number);
  }
  
  public int[] getClassRefArray(){
    int max_type = 0;
    for(int num : m_classRefToTypeNumber.values()){
      if(num > max_type){
        max_type = num;
      }
    }
    int[] ret = new int[max_type+1];
    for(long value : m_classRefToTypeNumber.keySet()){
      int pos = m_classRefToTypeNumber.get(value);
      ret[pos] = (int) (value >> 4);
    }
    return ret;
  }
  
  private static synchronized WriteCacheResult checkWriteCache(Object o, int size, boolean read_only, Memory mem){
    //strings are cached in Java 1.6, we need to make strings individual units
    //for rootbeer so concurrent modifications change different objects
    if(o instanceof String){
      long ref = mem.mallocWithSize(size);
      return new WriteCacheResult(ref, true);
    } else {
      if(mWriteToGpuCache.containsKey(o)){
        long ref = mWriteToGpuCache.get(o);
        return new WriteCacheResult(ref, false);
      }
      long ref = mem.mallocWithSize(size);
      mWriteToGpuCache.put(o, ref);
      mReverseWriteToGpuCache.put(ref, o);
      return new WriteCacheResult(ref, true);
    }
  }
  
  public Object writeCacheFetch(long ref){
    synchronized(mWriteToGpuCache){
      if(mReverseWriteToGpuCache.containsKey(ref)){
        return mReverseWriteToGpuCache.get(ref);
      }
      return null;
    }
  }
  
  public long writeToHeap(Object o, boolean write_data){
    if(o == null)
      return -1;    
    int size = doGetSize(o);
    boolean read_only = false;
    WriteCacheResult result;
    result = checkWriteCache(o, size, read_only, mMem);
    
    if(result.m_NeedToWrite == false){
      return result.m_Ref;
    }
    //if(o == null){
    //  System.out.println("writeToHeap: null at addr: "+result.m_Ref);
    //} else {
    //  System.out.println("writeToHeap: "+o.toString()+" at addr: "+result.m_Ref);
    //}
    doWriteToHeap(o, write_data, result.m_Ref, read_only);
    return result.m_Ref;
  }
  
  protected Object checkCache(long address, Object item){
    synchronized(mReadFromGpuCache){
      if(mReadFromGpuCache.containsKey(address)){
        return mReadFromGpuCache.get(address);
      } else {
        mReadFromGpuCache.put(address, item);
        return item;
      }
    }
  }

  public Object readFromHeap(Object o, boolean read_data, long address){
    synchronized(mReadFromGpuCache){
      if(mReadFromGpuCache.containsKey(address)){
        Object ret = mReadFromGpuCache.get(address);  
        return ret;
      }
    }
    long null_ptr_check = address >> 4;
    if(null_ptr_check == -1){
      return null;
    }
    //if(o == null){
    //  System.out.println("readFromHeap: null. addr: "+address);
    //} else {
    //  System.out.println("readFromHeap: "+o.toString()+". addr: "+address);
    //}
    //BufferPrinter printer = new BufferPrinter();
    //printer.print(mMem, address, 128);
    
    Object ret = doReadFromHeap(o, read_data, address);
    return checkCache(address, ret);
  }

  public void readStaticsFromHeap(){
    doReadStaticsFromHeap();
  }
 
  public Object readField(Object base, String name){
{'fraction_non_alphanumeric': 0.10706217463903349, 'fraction_numerical': 0.0029466653570376193, 'mean_word_length': 3.349423323366083, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 31, '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': '1212517', 'n_tokens_mistral': 3355, 'n_tokens_neox': 3343, 'n_words': 731}