Esempio n. 1
0
unsigned int sample_int(unsigned int a, unsigned int b)
{
    static_assert(random_engine.min() == 0, "Error");
    unsigned int num = static_cast<unsigned int>(ceil(sample_unif()*(b - a + 1))) + a - 1;
    // static_assert(a <= num <= b);
    return num;
}
Esempio n. 2
0
void sampleWeights(NeuralNet *net) {
    for(int layerId = 0; layerId < net->getNumLayers();  layerId++) {
        Layer *layer = net->getLayer(layerId);
        FullyConnectedLayer *fc = dynamic_cast< FullyConnectedLayer * >(layer);
        ConvolutionalLayer *conv = dynamic_cast< ConvolutionalLayer * >(layer);
        if(fc != 0) {
            conv = fc->convolutionalLayer;
        }
        if(conv == 0) {
            continue;
        }

        cout << "layer " << layerId << endl;
        float const*weights = conv->getWeights();
        conv->getBias();
        LayerDimensions &dim = conv->dim;
        int numFilters = dim.numFilters;
        int inputPlanes = dim.inputPlanes;
        int filterSize = dim.filterSize;

        initrand.seed(0);        
        for(int i = 0; i < 10; i++) {
            int thisrand = abs((int)initrand());
            int seq = thisrand % (numFilters * inputPlanes * filterSize * filterSize);
            int planefilter = seq / (filterSize * filterSize);
            int rowcol = seq % (filterSize * filterSize);
            int filter = planefilter / inputPlanes;
            int inputPlane = planefilter % inputPlanes;
            int row = rowcol / filterSize;
            int col = rowcol % filterSize;
            cout << "weights[" << filter << "," << inputPlane << "," << row << "," << col << "]=" << weights[ seq ] << endl;
        }
    }
}
// Main entry point to the program.
//
// Specifying a number argument allows to control number of iterations done in the program
// $1 - number of iteration to be performed
int main(int argc, char* argv[])
{
    // Determine the number of iterations to be done using user input
    long total_iterations = 0;

    if (argc == 2)
    {
        char *input = argv[1];
        total_iterations = strtol(input, NULL, 10);
    }

    if (total_iterations == 0)
    {
        total_iterations = DEFAULT_ITERATIONS_COUNT;
    }

    // Randomize
    random_device rd;
    mt_engine.seed(rd());

    // Initialize MPI and basic variables
    MPI_Init(&argc, &argv);

    double tic = MPI_Wtime();

    int world_size;
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);

    int current_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &current_rank);

    // Run the main process
    perform_process(total_iterations, current_rank, world_size);

    // Finish and produce summary
    double toc = MPI_Wtime();
    MPI_Finalize();

    if (current_rank == 0)
    {
        cout << "--- Operation took " << (toc - tic) << " seconds ---" << endl;
        system("pause");
    }

    return 0;
}
Esempio n. 4
0
void go(Config config) {
    Timer timer;

    int Ntrain;
    int Ntest;
    int numPlanes;
    int imageSize;

    float *trainData = 0;
    float *testData = 0;
    int *trainLabels = 0;
    int *testLabels = 0;

    int trainAllocateN = 0;
    int testAllocateN = 0;

//    int totalLinearSize;
    GenericLoader::getDimensions((config.dataDir + "/" + config.trainFile).c_str(), &Ntrain, &numPlanes, &imageSize);
    Ntrain = config.numTrain == -1 ? Ntrain : config.numTrain;
//    long allocateSize = (long)Ntrain * numPlanes * imageSize * imageSize;
    cout << "Ntrain " << Ntrain << " numPlanes " << numPlanes << " imageSize " << imageSize << endl;
    trainAllocateN = Ntrain;
    trainData = new float[ (long)trainAllocateN * numPlanes * imageSize * imageSize ];
    trainLabels = new int[trainAllocateN];
    if(Ntrain > 0) {
        GenericLoader::load((config.dataDir + "/" + config.trainFile).c_str(), trainData, trainLabels, 0, Ntrain);
    }

    GenericLoader::getDimensions((config.dataDir + "/" + config.validateFile).c_str(), &Ntest, &numPlanes, &imageSize);
    Ntest = config.numTest == -1 ? Ntest : config.numTest;
    testAllocateN = Ntest;
    testData = new float[ (long)testAllocateN * numPlanes * imageSize * imageSize ];
    testLabels = new int[testAllocateN]; 
    if(Ntest > 0) {
        GenericLoader::load((config.dataDir + "/" + config.validateFile).c_str(), testData, testLabels, 0, Ntest);
    }
    
    timer.timeCheck("after load images");

    const int inputCubeSize = numPlanes * imageSize * imageSize;
    float translate;
    float scale;
    int normalizationExamples = config.normalizationExamples > Ntrain ? Ntrain : config.normalizationExamples;
    if(config.normalization == "stddev") {
        float mean, stdDev;
        NormalizationHelper::getMeanAndStdDev(trainData, normalizationExamples * inputCubeSize, &mean, &stdDev);
        cout << " image stats mean " << mean << " stdDev " << stdDev << endl;
        translate = - mean;
        scale = 1.0f / stdDev / config.normalizationNumStds;
    } else if(config.normalization == "maxmin") {
        float mean, stdDev;
        NormalizationHelper::getMinMax(trainData, normalizationExamples * inputCubeSize, &mean, &stdDev);
        translate = - mean;
        scale = 1.0f / stdDev;
    } else {
        cout << "Error: Unknown normalization: " << config.normalization << endl;
        return;
    }
    
    cout << " image norm translate " << translate << " scale " << scale << endl;
    timer.timeCheck("after getting stats");

//    const int numToTrain = Ntrain;
//    const int batchSize = config.batchSize;
    EasyCL *cl = new EasyCL();
    NeuralNet *net = new NeuralNet(cl);
//    net->inputMaker<unsigned char>()->numPlanes(numPlanes)->imageSize(imageSize)->insert();
    net->addLayer(InputLayerMaker::instance()->numPlanes(numPlanes)->imageSize(imageSize));
    net->addLayer(NormalizationLayerMaker::instance()->translate(translate)->scale(scale));
    if(!NetdefToNet::createNetFromNetdef(net, config.netDef)) {
        return;
    }
    net->print();
    for(int i = 1; i < net->getNumLayers() - 1; i++) {
        Layer *layer = net->getLayer(i);
        FullyConnectedLayer *fc = dynamic_cast< FullyConnectedLayer * >(layer);
        ConvolutionalLayer *conv = dynamic_cast< ConvolutionalLayer * >(layer);
        if(fc != 0) {
            conv = fc->convolutionalLayer;
        }
        if(conv == 0) {
            continue;
        }
        initrand.seed(0);
        int weightsSize = conv->getWeightsSize();
    //int weightsSize = layer->getPersistSize();
        if(weightsSize > 0) {
            cout << "weightsSize " << weightsSize << endl;
            float *weights = new float[weightsSize];
            for(int j = 0; j < weightsSize; j++) {
                int thisrand = (int)initrand();
                float thisweight = (thisrand % 100000) / 1000000.0f;
                weights[j] = thisweight;
            }        
            conv->initWeights(weights);
        }
        if(conv->dim.biased) {
            initrand.seed(0);
            int biasedSize = conv->getBiasSize();
            float *biasWeights = new float[biasedSize];
            for(int j = 0; j < biasedSize; j++) {
                int thisrand = (int)initrand();
                float thisweight = (thisrand % 100000) / 1000000.0f;
                biasWeights[j] = thisweight;
                //biasWeights[j] = 0;
            }        
            conv->initBias(biasWeights);
        }
    }

    cout << "weight samples before learning:" << endl;
    sampleWeights(net);

    bool afterRestart = false;
    int restartEpoch = 0;
//    int restartBatch = 0;
//    float restartAnnealedLearningRate = 0;
//    int restartNumRight = 0;
//    float restartLoss = 0;

    timer.timeCheck("before learning start");
    if(config.dumpTimings) {
        StatefulTimer::dump(true);
    }
    StatefulTimer::timeCheck("START");

    SGD *sgd = SGD::instance(cl, config.learningRate, 0.0f);
    Trainable *trainable = net;
    NetLearner netLearner(
        sgd, trainable,
        Ntrain, trainData, trainLabels,
        Ntest, testData, testLabels,
        config.batchSize);
    netLearner.setSchedule(config.numEpochs, afterRestart ? restartEpoch : 1);
//    netLearner.setBatchSize(config.batchSize);
    netLearner.setDumpTimings(config.dumpTimings);
//    netLearner.learn(config.learningRate, 1.0f);

    cout << "forward output" << endl;
    for(int layerId = 0; layerId < net->getNumLayers(); layerId++) {
        Layer *layer = net->getLayer(layerId);
        FullyConnectedLayer *fc = dynamic_cast< FullyConnectedLayer * >(layer);
        ConvolutionalLayer *conv = dynamic_cast< ConvolutionalLayer * >(layer);
        PoolingLayer *pool = dynamic_cast< PoolingLayer * >(layer);
        SoftMaxLayer *softMax = dynamic_cast< SoftMaxLayer * >(layer);
        if(fc != 0) {
            conv = fc->convolutionalLayer;
        }
        int planes = 0;
        int imageSize = 0;
        if(conv != 0) {
            cout << "convolutional (or conv based, ie fc)" << endl;
            planes = conv->dim.numFilters;
            imageSize = conv->dim.outputSize;
          //  continue;
        } else if(pool != 0) {
            cout << "pooling" << endl;
            planes = pool->numPlanes;
            imageSize = pool->outputSize;
        } else if(softMax != 0) {
            cout << "softmax" << endl;
            planes = softMax->numPlanes;
            imageSize = softMax->imageSize;
        } else {
            continue;
        }
        cout << "layer " << layerId << endl;
//        conv->getOutput();
        float const*output = layer->getOutput();
//        for(int i = 0; i < 3; i++) {
//            cout << conv->getOutput()[i] << endl;
//        }
        initrand.seed(0);
//        LayerDimensions &dim = conv->dim;
        for(int i = 0; i < 10; i++) {
            int thisrand = abs((int)initrand());
            int seq = thisrand % (planes * imageSize * imageSize);
            int outPlane = seq / (imageSize * imageSize);
            int rowcol = seq % (imageSize * imageSize);
            int row = rowcol / imageSize;
            int col = rowcol % imageSize;
            cout << "out[" << outPlane << "," << row << "," << col << "]=" << output[ seq ] << endl;
        }
    }

    cout << "weight samples after learning:" << endl;
    sampleWeights(net);

    cout << "backprop output" << endl;
    for(int layerId = net->getNumLayers() - 1; layerId >= 0; layerId--) {
        Layer *layer = net->getLayer(layerId);
        FullyConnectedLayer *fc = dynamic_cast< FullyConnectedLayer * >(layer);
        ConvolutionalLayer *conv = dynamic_cast< ConvolutionalLayer * >(layer);
        if(fc != 0) {
            conv = fc->convolutionalLayer;
        }
        if(conv == 0) {
            continue;
        }

        cout << "layer " << layerId << endl;
        float const*weights = conv->getWeights();
        float const*biases = conv->getBias();
        int weightsSize = conv->getWeightsSize() / conv->dim.numFilters;
        for(int i = 0; i < weightsSize; i++) {
            cout << " weight " << i << " " << weights[i] << endl;
        }
        for(int i = 0; i < 3; i++) {
            cout << " bias " << i << " " << biases[i] << endl;
        }
    }
    cout << "done" << endl;

    delete sgd;
    delete net;
    delete cl;

    if(trainData != 0) {
        delete[] trainData;
    }
    if(testData != 0) {
        delete[] testData;
    }
    if(testLabels != 0) {
        delete[] testLabels;
    }
    if(trainLabels != 0) {
        delete[] trainLabels;
    }
}
Esempio n. 5
0
void init(int argv, char **argc){
  char cwd[1024];
  FILE *fp;
  long seed, *allseeds;

  // Initialize MPI
#ifdef WITHMPI
  MPI_Init(&argv,&argc);
  MPI_Comm_rank(MPI_COMM_WORLD, &global.myid);
  MPI_Comm_size(MPI_COMM_WORLD, &global.ncpu);
#else
  global.myid = 0;
  global.ncpu = 1;
#endif

  // Check we have enough input
  if(argv < 7){
    cout << "Error in init. Not enough data: " << endl;;
    for(int i=0;i<argv;i++) cout << i << " : " << argc[i] << endl;
    myexit(5,"init");
  }

  // Initialize random number generator. Set random seed using urandom or by time + id if not availiable
#ifdef CXXRANDOM
  seed = rd();
  rg.seed(seed);
#else
  if((fp = fopen("/dev/urandom","r")) != NULL){
    fread(&seed, sizeof(int), 1, fp);
    fclose(fp);
  } else {
    seed = time(NULL) + 10 * (global.myid + 1);
  }
  srand(seed);
#endif

  // Fetch all random seeds to allow for reproducibillity of the results
  if(global.myid==0) allseeds = new long[global.ncpu];
#ifdef WITHMPI
  MPI_Gather(&seed,1,MPI_LONG,allseeds,1,MPI_LONG,0,MPI_COMM_WORLD);
#endif
  if(global.myid==0){
    cout << " -- Random seeds used by different CPUs: " << endl;
    for(int i=0;i<global.ncpu;i++) cout << " -- id = " << i << " seed = " << allseeds[i] << endl;
    cout << endl;
    delete[] allseeds;
  }

  // Read filepath and ramses output nr
  global.filepath = argc[1];
  global.filenum  = atoi(argc[2]);
  global.nfiles   = atoi(argc[3]);
  if(global.filepath == ".") global.filepath = getcwd(cwd, sizeof(cwd));
 
  // Allocate and initialize grid for density field (and later epsilon = F{delta}/|F{delta}|)
  global.allocate_grid(NGRID_HARDCODED);

  // Boxsize and rmax
  global.boxsize = atoi(argc[5]);

  // Make binning for l(r): npoints from rmin to rmax
  global.allocate_bins(atoi(argc[6]), 1.0/double(global.ngrid/2), 0.5);

  // Output filename
  global.outputprefix = argc[7];

  // Verbose
  if(global.myid==0){
    cout << "Initializing" << endl;
    cout << " -- Running with    " << global.ncpu                << " CPUs" << endl;
    cout << " -- RAMSES files    " << global.filepath            << " " << global.filenum << " " << global.nfiles << endl;
    cout << " -- Gridsize        " << global.ngrid               << endl;
    cout << " -- Nparticles      " << atoi(argc[4])              << endl;
    cout << " -- Rmin (Mpc/h)    " << global.boxsize*global.rmin << endl;
    cout << " -- Rmax (Mpc/h)    " << global.boxsize*global.rmax << endl;
    cout << " -- Box  (Mpc/h)    " << global.boxsize             << endl;
    cout << " -- Nbins           " << global.nbins               << endl;
    cout << " -- Outputprefix    " << global.outputprefix         << endl;
  }

  // Read and bin particles
  read_and_bin_particles(atoi(argc[4]), global.filepath, global.filenum, global.nfiles);

  // Precompute j0(2pi Sqrt(x)). If not called we compute it directly
#ifdef USEBESSELLOOKUP
  precompute_j0(NPOINTS_J0_PRECOMP, BESSELXMAX);
#endif

  // Allocate memory for pofk and zeta(r) binning. 
  // If not called it will not be computed!
  int kmax = int(NGRID_HARDCODED/2 * sqrt(3.0)) + 1;
  int ncorr_ptr = 50;
  global.allocate_pofk(kmax, ncorr_ptr);
}
Esempio n. 6
0
 Sampler(xg::XG* x, int seed = 0, bool forward_only = false) : xgidx(x), node_cache(100), forward_only(forward_only), nonce(0) {
     if (!seed) {
         seed = time(NULL);
     }
     rng.seed(seed);
 }
Esempio n. 7
0
double sample_unif()
{
    static_assert(random_engine.min() == 0, "Error");
    return static_cast<double>(random_engine()) / random_engine.max();
}
Esempio n. 8
0
void sample_seed(unsigned int s)
{
    random_engine.seed(s);
}
Esempio n. 9
0
int main() {

    chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
    gen.seed(t1.time_since_epoch().count());

    Group Academy = readGroupFromFile("pseudo.txt");


    for(int i = 1; i <= 27; i++) {
        Academy.createClass(i);
    }

    //Academy = assignClasses(Academy);

    //_beginthread(worker,0,&Academy);

    unsigned int minConflicts = INF;
    //ofstream w("dump.txt",ios_base::out);
    unsigned int temp;
    Group tempGroup,minGroup;
    unsigned int cycles = 5;
    for(int i = 0; i < cycles; i++) {
        //tempGroup = scheduleClasses(randomizeGroup(Academy));
        tempGroup = scheduleClassesAlt(randomizeGroup(Academy));
        //tempGroup = randomScheduleClasses(Academy);
        temp = getConflicts(tempGroup);

        /*
        for(int i = 0; i < tempGroup.Students.size()-290; i++) {
            w << tempGroup.Students[i].getID() << "\t";
        }
        w << endl;
        */


        //w << temp << endl;
        if (temp < minConflicts) {
            minConflicts = temp;
            minGroup = tempGroup;
            cout << minConflicts << endl;
        }
        //cout << "\r" << i;
    }
    cout << endl;
    cout << minConflicts << endl << endl;

    /*
    for(int i = 0; i < minGroup.Classes.size(); i++) {
        cout << "C" << minGroup.Classes[i].getID() << "\t";
        for(int j = 0; j < minGroup.Classes[i].Sections.size(); j++) {
            cout << minGroup.Classes[i].Sections[j].period << "\t";
        }
        cout << endl;
    }
    */

    chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
    chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
    cout << endl << "OPS = " << (float)cycles/time_span.count() << endl;


    for(int i = 0; i < minGroup.Classes.size(); i++) {
        for(int j = 0; j < minGroup.Classes[i].Sections.size(); j++) {
            cout << minGroup.Classes[i].getID()
                 << "."
                 << minGroup.Classes[i].Sections[j].period
                 << "\t"
                 << minGroup.Classes[i].Sections[j].Roster.size()
                 << endl;
        }
    }

    getchar();

    return 0;
}
Esempio n. 10
0
 Pictographs(int seed_val) {
     rng.seed(seed_val);
 };