<commit_before>// Static scheduling demo - mapper part.
//
// Copyright (C) <NAME>, 2016.

#include <stdarg.h>

#include "legion.h"
#include "realm.h"
#include "legion_types.h"
#include "default_mapper.h"
#include "id.h"

#include "static-scheduling-support.h"

using namespace Realm;
using namespace Legion;
using namespace Legion::Mapping;
using namespace LegionRuntime::Accessor;


LegionRuntime::Logger::Category log_logging("logging");

void node_log(char*fmt, ...) {
    va_list vl;
    char buf[1024];
    va_start(vl, fmt);
    vsprintf(buf, fmt, vl);
    //log_logging.print("%s", buf);
    printf("%s\n", buf); fflush(stdout);
} /* node_log */


class StaticSchedulingMapper : public DefaultMapper {
public:
  StaticSchedulingMapper(MapperRuntime *rt, Machine machine, Processor local, 
                         const char *mapper_name = NULL)
    : DefaultMapper(rt, machine, local, mapper_name)
  {
  }

public:
  virtual Processor default_policy_select_initial_processor(
                                    MapperContext ctx, const Task &task);
//  virtual void slice_domain(const Task *task, const Domain &domain,
//                            std::vector<DomainSplit> &slices);
//  virtual bool map_task(Task *task);
private:
  bool equal_to(std::string& a, char*b) {
    return a == std::string(b);
  }
  // compute CPU index for a task from task's ID.
  Processor task_cpu(const Task *task, Processor old_proc) {
    std::set<Processor> all_procs;
    machine.get_all_processors(all_procs);
    std::string taskname(task->get_task_name());
    int cpu = -1;
    int i = -1;
    int j = -1;
    int* args = (int*)task->args;
    unsigned node_to_search = -1;
    node_log("task name %s, arglen %d", taskname.c_str(), task->arglen);
    if (task->arglen >= sizeof(int)) {
        cpu = args[0];
    }
    if (task -> arglen >= sizeof(int)*2) {
        i = args[1];
    }
    if (task -> arglen >= sizeof(int)*3) {
        j = args[2];
    }
    if (equal_to(taskname, "level_0_task")) {
        assert(cpu >= 0 && cpu < 4);
        node_to_search = cpu;
    }
    else if (equal_to(taskname, "level_1_task")) {
        assert(i >= 0 && i < 2);
        assert(cpu >= 0 && cpu < 4);
        node_to_search = 4+cpu+i*4;
    }
    else if (equal_to(taskname, "level_2_task")) {
        assert(i >= 0 && i < 2);
        assert(cpu >= 0 && cpu < 4);
        assert(j >= 0 && j < 5);
        node_to_search = 12+cpu+j*4;
    }
    for (std::set<Processor>::const_iterator it = all_procs.begin();
            it != all_procs.end(); it++) {
        Realm::ID cpuid(*it);
        if (it->kind() == Processor::LOC_PROC && cpuid.node() == node_to_search) { // selecting node we need.
            return (*it);
        }
    }

    return old_proc;
  }
};

Processor StaticSchedulingMapper::default_policy_select_initial_processor(
                                    MapperContext ctx, const Task &task)<commit_msg>Guarded reading of Regent's task arguments.<commit_after>// Static scheduling demo - mapper part.
//
// Copyright (C) <NAME>, 2016.

#include <stdarg.h>

#include "legion.h"
#include "realm.h"
#include "legion_types.h"
#include "default_mapper.h"
#include "id.h"

#include "static-scheduling-support.h"

using namespace Realm;
using namespace Legion;
using namespace Legion::Mapping;
using namespace LegionRuntime::Accessor;


LegionRuntime::Logger::Category log_logging("logging");

void node_log(char*fmt, ...) {
    va_list vl;
    char buf[1024];
    va_start(vl, fmt);
    vsprintf(buf, fmt, vl);
    //log_logging.print("%s", buf);
    printf("%s\n", buf); fflush(stdout);
} /* node_log */


class StaticSchedulingMapper : public DefaultMapper {
public:
  StaticSchedulingMapper(MapperRuntime *rt, Machine machine, Processor local, 
                         const char *mapper_name = NULL)
    : DefaultMapper(rt, machine, local, mapper_name)
  {
  }

public:
  virtual Processor default_policy_select_initial_processor(
                                    MapperContext ctx, const Task &task);
//  virtual void slice_domain(const Task *task, const Domain &domain,
//                            std::vector<DomainSplit> &slices);
//  virtual bool map_task(Task *task);
private:
  bool equal_to(std::string& a, char*b) {
    return a == std::string(b);
  }
  // compute CPU index for a task from task's ID.
  Processor task_cpu(const Task *task, Processor old_proc) {
    std::set<Processor> all_procs;
    machine.get_all_processors(all_procs);
    std::string taskname(task->get_task_name());
    int cpu = -1;
    int i = -1;
    int j = -1;

    int* args = (int*)task->args.base();
    unsigned arglen = task->args.size();

    unsigned node_to_search = -1;

    node_log("task name %s, arglen %d", taskname.c_str(), task->arglen);
    if (arglen >= sizeof(int)) {
        cpu = args[0];
    }
    if (arglen >= sizeof(int)*2) {
        i = args[1];
    }
    if (arglen >= sizeof(int)*3) {
        j = args[2];
    }
    if (equal_to(taskname, "level_0_task")) {
        assert(cpu >= 0 && cpu < 4);
        node_to_search = cpu;
    }
    else if (equal_to(taskname, "level_1_task")) {
        assert(i >= 0 && i < 2);
        assert(cpu >= 0 && cpu < 4);
        node_to_search = 4+cpu+i*4;
    }
    else if (equal_to(taskname, "level_2_task")) {
        assert(i >= 0 && i < 2);
        assert(cpu >= 0 && cpu < 4);
        assert(j >= 0 && j < 5);
        node_to_search = 12+cpu+j*4;
    }
    for (std::set<Processor>::const_iterator it = all_procs.begin();
            it != all_procs.end(); it++) {
        Realm::ID cpuid(*it);
        if (it->kind() == Processor::LOC_PROC && cpuid.node() == node_to_search) { // selecting node we need.
            return (*it);
        }
    }

    return old_proc;
  }
};

Processor StaticSchedulingMapper::default_policy_select_initial_processor(
                                    MapperContext ctx, const Task &task)
{'fraction_non_alphanumeric': 0.14787179921994234, 'fraction_numerical': 0.012548753603527217, 'mean_word_length': 2.667910447761194, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7253040', 'n_tokens_mistral': 2041, 'n_tokens_neox': 1947, 'n_words': 473}