Exemplo n.º 1
0
int filterA::process(){

	cout << "process " << get_filter_name() << " Id: " 
		<< get_global_filter_thread_id() << ". Rank: " << get_my_rank() << "\n";

	DCBuffer * outBuffer = new DCBuffer(100);
	outBuffer->pack("s", "Hellow!!!");

	write (outBuffer, "out");
	outBuffer->Delete();

	DCBuffer * outBuffer1 = new DCBuffer(100);
	outBuffer1->pack("s", "NonBlocking message.");

	write_nocopy(outBuffer1, "out");
	outBuffer1->Delete();	

	//filter_exit("Nao e' nada nao");

	return 0;
}
void ocvm_tessellator::do_tess(DCBuffer * input)
{
    int4 sentinel;
    int8 width, height;
    int4 x, y, z;
    uint1 threshold;
    int4 color;
    input->unpack("iBlliiii", &sentinel, &threshold,
                  &width, &height, &x, &y, &z,
                  &color);
    const int hdrsz = 64;
    input->resetExtract(hdrsz);
    int8 cells_per_row = width / user_tessellation_x;
    if (width % user_tessellation_x) {
        cells_per_row++;
    }
    int8 total_cell_rows = height / user_tessellation_y;
    if (height % user_tessellation_y) {
        total_cell_rows++;
    }
    int new_sz = cells_per_row * total_cell_rows*sizeof(ocvm_sum_integer_type);
    DCBuffer * out = new DCBuffer(
        hdrsz + new_sz);
    memset(out->getPtr(), 0, hdrsz + new_sz);
    out->pack("iBlliiii", sentinel, threshold, cells_per_row, total_cell_rows,
              x, y, z, color);
//     std::cout << "tessellation: active on x,y,z of "
//               << x << "," << y << "," << z << ", color " << color
//               << " on host " << dcmpi_get_hostname(true) << endl;
    out->setUsedSize(hdrsz + new_sz);
    uint1 * src_base = (uint1*)input->getPtrExtract();
    uint1 * src = src_base;
    uint1 * src_end = src_base + width * height;
    ocvm_sum_integer_type * dest = (ocvm_sum_integer_type*)(out->getPtr()+hdrsz);
    ocvm_sum_integer_type * dest_end = (ocvm_sum_integer_type*)((char*)dest + new_sz);
    register ocvm_sum_integer_type count;
    int8 input_rows_covered = 0;
    int cell, cell_member;
    while (input_rows_covered < height) {
        assert (dest < dest_end);
        uint1 * row_end = src + width;
        for (cell = 0; cell < cells_per_row; cell++) {
            memcpy(&count, dest + cell, sizeof(ocvm_sum_integer_type));
            for (cell_member = 0;
                 cell_member < user_tessellation_x && src < row_end;
                 cell_member++) {
                count += *src;
                src++;
            }
            memcpy(dest + cell, &count, sizeof(ocvm_sum_integer_type));
        }
        input_rows_covered++;
        if (input_rows_covered % user_tessellation_y == 0) {
            dest += cells_per_row;
        }
    }
//     cout << "te2: " << *out << endl;
//     std::cout << "tess sending out ";
//     out->extended_print(cout);
    write_nocopy(out, "0");
    delete input;
}
int main(int argc, char *argv[])
{

    uint warp_filters_per_host = 1;
    std::string dest_host_scratch_filename;
    bool non_local_destination = 0;
    std::string client_hosts_filename;
    bool non_local_clients = 0;

    while (argc > 1) {
        if (!strcmp(argv[1], "-w")) {
                 warp_filters_per_host = atoi(argv[2]);
                 dcmpi_args_shift(argc, argv);
        }
        else if (!strcmp(argv[1], "-clients")) {
                 non_local_clients = 1;
                 client_hosts_filename = argv[2];
                 dcmpi_args_shift(argc, argv);
        }
        else if (!strcmp(argv[1], "-dest")) {
                 non_local_destination = 1;
                 dest_host_scratch_filename = argv[2];
                 dcmpi_args_shift(argc, argv);
        }
        else {
            break;
        }
        dcmpi_args_shift(argc, argv);
    }

    if ((argc-1) != 3) {
        appname = argv[0];
        usage();
    }

    HostScratch *dest_host_scratch = NULL;
    if (non_local_destination) {
        dest_host_scratch = new HostScratch(dest_host_scratch_filename);
    }
    if (non_local_destination && dest_host_scratch->components.empty()) {
        std::cerr << "ERROR:  destination host file is empty, aborting"
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }

    HostScratch *client_host_scratch = NULL;
    if (non_local_clients) {
        client_host_scratch = new HostScratch(client_hosts_filename);
    }
    if (non_local_clients && client_host_scratch->components.empty()) {
        std::cerr << "ERROR:  destination host file is empty, aborting"
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }

    if (!dcmpi_string_ends_with(tostr(argv[1]), ".xml")) {
        std::cerr << "ERROR: invalid filename " << tostr(argv[1])
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }
    JibberXMLDescriptor *jxd = new JibberXMLDescriptor();
    jxd->init_from_file(argv[1]);
    cout << "delta = " << jxd->delta << endl;
    cout << "ncp = " << jxd->num_control_points << endl;

    if (!dcmpi_string_ends_with(tostr(argv[2]), ".dim")) {
        std::cerr << "ERROR: invalid filename " << tostr(argv[2])
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }
    if (!dcmpi_string_ends_with(tostr(argv[3]), ".dim")) {
        std::cerr << "ERROR: invalid filename " << tostr(argv[3])
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }
    std::string warp_filename = tostr(argv[3]);

    uint u, u2;
    int rc;
    ImageDescriptor descriptor;
    std::string image_descriptor_string = file_to_string(argv[2]);
    descriptor.init_from_string(image_descriptor_string);
    std::vector<std::string> input_hosts = descriptor.get_hosts();
    std::vector<std::string> hosts;

    std::map< std::string, std::string> src_to_dest_host, client_to_dest_host, client_to_src_host, src_to_client_host;
    if (non_local_destination && !non_local_clients) {
        assert(input_hosts.size() == dest_host_scratch->components.size());     // Assumption for now. Will change later
        for (u = 0; u < dest_host_scratch->components.size(); u++) {
            src_to_dest_host[input_hosts[u]] = (dest_host_scratch->components[u])[0];
        }
    }

    if (non_local_clients) {
        assert(input_hosts.size() == client_host_scratch->components.size());   // Assumption for now. Will change later
        for (u = 0; u < client_host_scratch->components.size(); u++) {
            client_to_src_host[(client_host_scratch->components[u])[0]] = input_hosts[u];
            src_to_client_host[input_hosts[u]] = (client_host_scratch->components[u])[0];
        }
        if (non_local_destination) {
            assert(client_host_scratch->components.size() == dest_host_scratch->components.size());     // Assumption for now. Will change later
            for (u = 0; u < client_host_scratch->components.size(); u++) {
                client_to_dest_host[(client_host_scratch->components[u])[0]] = (dest_host_scratch->components[u])[0];
            }
        }
        hosts = client_host_scratch->get_hosts();
    }
    else {
        hosts = input_hosts;
    }

    DCLayout layout;
    layout.use_filter_library("libocvmfilters.so");
    DCFilterInstance console ("<console>", "console");
    layout.add(console);

    std::vector< std::string> dest_hosts;
    std::vector< std::string> client_hosts;
    if (non_local_destination) {
        dest_hosts = dest_host_scratch->get_hosts();
    }
    if (non_local_clients) {
        client_hosts = client_host_scratch->get_hosts();
    }
    MediatorInfo info = mediator_setup(layout, 2, 1, input_hosts, client_hosts, dest_hosts);

    std::vector<DCFilterInstance*> computers;
    std::vector<DCFilterInstance*> mappers;
    std::vector<DCFilterInstance*> readalls;
    std::vector<DCFilterInstance*> mapper_readalls;
    std::vector<DCFilterInstance*> writers;
    for (u = 0; u < hosts.size(); u++) {
        for (u2 = 0; u2 < warp_filters_per_host; u2++) {
            std::string uniqueName = "W1_" + tostr(u) + "_" + tostr(u2);
            DCFilterInstance * warper =
                new DCFilterInstance("ocvm_warper", uniqueName);
            layout.add(warper);
            computers.push_back(warper);
            warper->bind_to_host(hosts[u]);
            warper->set_param("algo", "naive");
            warper->set_param("label", uniqueName);
            warper->set_param("myhostname", hosts[u]);
            warper->set_param("threadID", tostr(u2));
            warper->set_param("warp_filters_per_host", warp_filters_per_host);
            warper->set_param("delta", tostr(jxd->delta));
            warper->set_param("num_control_points", tostr(jxd->num_control_points));
//            warper->set_param("image_descriptor_string", tostr(descriptor));
            if (non_local_clients && non_local_destination) {
                warper->set_param("dest_host_string", client_to_dest_host[hosts[u]]);
                warper->set_param("dest_scratchdir", dest_host_scratch->get_scratch_for_host(client_to_dest_host[hosts[u]]));
            }
            else if (non_local_destination && !non_local_clients) {
                warper->set_param("dest_host_string", src_to_dest_host[hosts[u]]);
                warper->set_param("dest_scratchdir", dest_host_scratch->get_scratch_for_host(src_to_dest_host[hosts[u]]));
            }
            else {
                warper->set_param("dest_host_string", hosts[u]);
                if (!non_local_clients) {
                    warper->set_param("dest_scratchdir", "");
                }
                else {
                    warper->set_param("dest_scratchdir", client_host_scratch->get_scratch_for_host(hosts[u]));
                }
            }

            layout.add_port(&console, "to_warper", warper, "from_console");

            if (u2 > 0) {
                continue;                               // only 1 warp mapper and writer per host
            }

            DCFilterInstance * mediator_readall =
                new DCFilterInstance("ocvm_mediator_readall_samehost",
                                     tostr("ra_") + tostr(hosts[u]));
            mediator_readall->bind_to_host(hosts[u]);
            readalls.push_back(mediator_readall);
//             mediator_readall->set_param("image_descriptor_string",
//                                         tostr(descriptor));
            layout.add(mediator_readall);

            DCFilterInstance * mapper_readall =
                new DCFilterInstance("ocvm_mediator_readall_samehost",
                                     tostr("mra_") + tostr(hosts[u]));
            mapper_readall->bind_to_host(hosts[u]);
            mapper_readalls.push_back(mapper_readall);
//             mapper_readall->set_param("image_descriptor_string",
//                                         tostr(descriptor));
            layout.add(mapper_readall);

            uniqueName = "WM_" + tostr(u);
            DCFilterInstance * warpmapper =
                new DCFilterInstance("ocvm_warp_mapper", uniqueName);
            layout.add(warpmapper);
            mappers.push_back(warpmapper);
            warpmapper->bind_to_host(hosts[u]);
            warpmapper->set_param("label", uniqueName);
            warpmapper->set_param("myhostname", hosts[u]);
            warpmapper->set_param("warp_filters_per_host", warp_filters_per_host);
//            warpmapper->set_param("image_descriptor_string", tostr(descriptor));

            layout.add_port(mapper_readall, "output",
                            warpmapper, "from_readall");
            layout.add_port(warpmapper, "ack",
                            mapper_readall, "ack");

            uniqueName = "WW_" + tostr(u);
            DCFilterInstance * warpwriter =
                new DCFilterInstance("ocvm_warp_writer", uniqueName);
            layout.add(warpwriter);
            writers.push_back(warpwriter);
            warpwriter->bind_to_host(hosts[u]);
            warpwriter->set_param("label", uniqueName);
            warpwriter->set_param("myhostname", hosts[u]);
            warpwriter->set_param("warp_filters_per_host", warp_filters_per_host);
//           warpwriter->set_param("image_descriptor_string", tostr(descriptor));
            if (non_local_clients && non_local_destination) {
                warpwriter->set_param("dest_host_string", client_to_dest_host[hosts[u]]);
                warpwriter->set_param("dest_scratchdir", dest_host_scratch->get_scratch_for_host(client_to_dest_host[hosts[u]]));
            }
            else if (non_local_destination && !non_local_clients) {
                warpwriter->set_param("dest_host_string", src_to_dest_host[hosts[u]]);
                warpwriter->set_param("dest_scratchdir", dest_host_scratch->get_scratch_for_host(src_to_dest_host[hosts[u]]));
            }
            else {
                warpwriter->set_param("dest_host_string", hosts[u]);
                if (!non_local_clients) {
                    warpwriter->set_param("dest_scratchdir", "");
                }
                else {
                    warpwriter->set_param("dest_scratchdir", client_host_scratch->get_scratch_for_host(hosts[u]));
                }
            }

            if (non_local_clients) {
                warper->set_param("input_hostname", client_to_src_host[hosts[u]]);
                warpmapper->set_param("input_hostname", client_to_src_host[hosts[u]]);
                warpwriter->set_param("input_hostname", client_to_src_host[hosts[u]]);
            }
            else {
                warper->set_param("input_hostname", hosts[u]);
                warpmapper->set_param("input_hostname", hosts[u]);
                warpwriter->set_param("input_hostname", hosts[u]);
            }
    
            layout.add_port(warpwriter, "to_console", &console, "from_warpwriter");
        }
    }

    for (u = 0; u < readalls.size(); u++) {
        for (u2 = 0; u2 < warp_filters_per_host; u2++) {
            layout.add_port(readalls[u], "output",
                            computers[u*warp_filters_per_host + u2], "from_readall");
            layout.add_port(computers[u*warp_filters_per_host + u2], "ack",
                            readalls[u], "ack");
        }
    }

    for (u = 0; u < computers.size(); u++) {
        for (u2 = 0; u2 < mappers.size(); u2++) {                                                       // assumes 1 mapper and 1 writer per host
            if (computers[u]->get_param("myhostname") == writers[u2]->get_param("myhostname")) {
                layout.add_port(computers[u], "to_" + writers[u2]->get_param("myhostname"), writers[u2], "0");
            }
            layout.add_port(computers[u], "to_m_" + mappers[u2]->get_param("myhostname"), mappers[u2], "0");
        }
    }
    
    for (u = 0; u < mappers.size(); u++) {
        for (u2 = 0; u2 < writers.size(); u2++) {
            layout.add_port(mappers[u], "to_" + writers[u2]->get_param("myhostname"), writers[u2], "0");
        }
    }

    mediator_add_client(layout, info, readalls);
    mediator_add_client(layout, info, mapper_readalls);
    mediator_add_client(layout, info, writers);

    double before = dcmpi_doubletime();

    std::string dim_timestamp = get_dim_output_timestamp();
    layout.set_param_all("dim_timestamp", dim_timestamp);

    DCFilter * console_filter = layout.execute_start();
    DCBuffer * imgstr = new DCBuffer(image_descriptor_string.size()+1);
    imgstr->pack("s", image_descriptor_string.c_str());
    console_filter->write_broadcast(imgstr, "to_warper");
    delete imgstr;
    for (u = 0; u < jxd->num_control_points; u++) {
        DCBuffer * out = new DCBuffer(4 * sizeof(float) + sizeof(uint));
/*
        cout << jxd->correspondences[u]->origin_x <<  " " << 
                           jxd->correspondences[u]->origin_y <<  " " <<
                           jxd->correspondences[u]->endpoint_x <<  " " <<
                           jxd->correspondences[u]->endpoint_y <<  " " <<
                           jxd->correspondences[u]->weight << endl;
*/

        out->pack("ffffi", jxd->correspondences[u]->origin_x, 
                           jxd->correspondences[u]->origin_y, 
                           jxd->correspondences[u]->endpoint_x, 
                           jxd->correspondences[u]->endpoint_y, 
                           jxd->correspondences[u]->weight);
        console_filter->write_broadcast(out, "to_warper");
    }

    std::map<ImageCoordinate, std::pair<std::string, int8> > newfiles;
    for (u = 0; u < descriptor.parts.size(); u++) {
        DCBuffer * in = console_filter->read("from_warpwriter");
        ImageCoordinate ic;
        std::string output_filename;
        int8 output_offset;
        in->unpack("iiisl", &ic.x, &ic.y, &ic.z,
                   &output_filename, &output_offset);
        newfiles[ic] = make_pair(output_filename, output_offset);
        delete in;
    }

    rc = layout.execute_finish();
    if (rc) {
        std::cerr << "ERROR: layout.execute() returned " << rc
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }
    else {
        std::string message = "type BGRplanar\n";
        message += "pixels_x " + tostr(descriptor.pixels_x) + "\n";
        message += "pixels_y " + tostr(descriptor.pixels_y) + "\n";
        message += "pixels_z " + tostr(descriptor.pixels_z) + "\n";
        message += "chunks_x " + tostr(descriptor.chunks_x) + "\n";
        message += "chunks_y " + tostr(descriptor.chunks_y) + "\n";
        message += "chunks_z " + tostr(descriptor.chunks_z) + "\n";
        message += "chunk_dimensions_x";
        for (u = 0; u < descriptor.chunks_x; u++) {
            message += " ";
            message += tostr(descriptor.chunk_dimensions_x[u]);
        }
        message += "\n";
        message += "chunk_dimensions_y";
        for (u = 0; u < descriptor.chunks_y; u++) {
            message += " ";
            message += tostr(descriptor.chunk_dimensions_y[u]);
        }
        message += "\n";
        for (u = 0; u < descriptor.parts.size(); u++) {
            ImagePart & part = descriptor.parts[u];
            std::string & fn_old = part.filename;
            std::string fn_new = newfiles[part.coordinate].first;
            int8 offset_new = newfiles[part.coordinate].second;
            std::string output_hostname = part.hostname;
            if (non_local_destination && non_local_clients) {
                output_hostname = client_to_dest_host[src_to_client_host[part.hostname]];
            }
            if (non_local_destination && !non_local_clients) {
                output_hostname = src_to_dest_host[part.hostname];
            }
            if (!non_local_destination && non_local_clients) {
                output_hostname = src_to_client_host[part.hostname];
            }
            message += "part " + tostr(part.coordinate) + " " +
                output_hostname + " " + fn_new + " " + tostr(offset_new) + "\n";
        }
        message += "timestamp " + dcmpi_get_time() + "\n";

        FILE *f_wdim;
        if ((f_wdim = fopen(warp_filename.c_str(), "w")) == NULL) {
            std::cerr << "ERROR: opening file"
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }
        if (fwrite(message.c_str(), message.size(), 1, f_wdim) < 1) {
            std::cerr << "ERROR: calling fwrite()"
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }
        if (fclose(f_wdim) != 0) {
            std::cerr << "ERROR: calling fclose()"
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }
    }

    double after = dcmpi_doubletime();
    std::cout << "elapsed warp " << (after - before) << " seconds" << endl;

    return rc;
}
int answer_ps_queries(
    DCFilter * console_filter,
    const std::vector<std::string> & hosts,
    ImageDescriptor original_image_descriptor,
    ImageDescriptor prefix_sum_descriptor,
    std::vector<PixelReq> query_points,
    int zslice,
    std::vector<std::vector<int8> > & results)
{
    
    DCBuffer keep_going;
    keep_going.pack("i", 1);
    console_filter->write_broadcast(&keep_going, "0");

    DCBuffer out;
    out.pack("ssi",
             "psquery",
             tostr(prefix_sum_descriptor).c_str(),
             zslice);
    std::cout << "zslice is " << zslice << endl;
    console_filter->write_broadcast(&out, "0");

    // build cache
    int tess_x, tess_y;
    std::string extra = prefix_sum_descriptor.extra;
    std::vector<std::string> toks = dcmpi_string_tokenize(extra);
    tess_x = Atoi(toks[1]);
    tess_y = Atoi(toks[3]);
    std::string lpscache = toks[5];
    toks = dcmpi_string_tokenize(lpscache, ":");
    // fill lower-right-corner cache
    std::map<std::string, std::string> lower_right_corner_cache;
    for (uint u = 0; u < toks.size(); u++) {
        const std::string & s = toks[u];
        std::vector<std::string> toks2 = dcmpi_string_tokenize(s, "/");
        lower_right_corner_cache[toks2[0]] = toks2[1];
        std::cout << "cache: " << toks2[0] << "->" << toks2[1] << endl;
    }
    
    std::map<PixelReq, std::vector<int> > cached_contributions;
    std::map<PixelReq, std::vector<PixelReq> > querypoint_pspoints;

#define INSERT(x,y) request_set.insert(PixelReq(x,y)); querypoint_pspoints[*it].push_back(PixelReq(x,y)); std::cout << "inserted " << x << "," << y << "\n";

    SerialSet<PixelReq> request_set;
    std::vector<PixelReq>::iterator it;
    for (it = query_points.begin();
         it != query_points.end();
         it++) {
        int8 psx, psy;
        int8 new_x, new_y;

        psx = it->x / tess_x;
        psy = it->y / tess_y;

        int chunk_x, chunk_y;
        prefix_sum_descriptor.pixel_to_chunk(psx, psy, chunk_x, chunk_y);
        if (chunk_x != 0 && chunk_y != 0) {
            chunk_x--;
            chunk_y--;
            std::string key =
                tostr(chunk_x) + "," +
                tostr(chunk_y) + "," +
                tostr(zslice);
            std::string val = lower_right_corner_cache[key];
            std::vector<std::string> toks = dcmpi_string_tokenize(val, ",");
            std::vector<int> bgrvals;
            for (uint u = 0; u < toks.size(); u++) {
                bgrvals.push_back(Atoi(toks[u]));
            }
            cached_contributions[*it] = bgrvals;
            std::cout << "cached_contribution for query point "
                      << *it << " is "
                      << val << endl;
        }


        while (1) {
            if (prefix_sum_descriptor.bottommost_pixel_next_chunk_up(
                    psx, psy, new_x, new_y) == false) {
                break;
            }
            INSERT(new_x, new_y);
            psx = new_x;
            psy = new_y;
        }

        psx = it->x / tess_x;
        psy = it->y / tess_y;
        while (1) {
            if (prefix_sum_descriptor.rightmost_pixel_next_chunk_to_the_left(
                    psx, psy, new_x, new_y) == false) {
                break;
            }
            INSERT(new_x, new_y);
            psx = new_x;
            psy = new_y;
        }
        psx = it->x / tess_x;
        psy = it->y / tess_y;
        INSERT(psx, psy);
    }
    std::cout << "request_set: ";
    std::copy(request_set.begin(), request_set.end(), ostream_iterator<PixelReq>(cout, " ")); cout << endl;


    DCBuffer pointsbuf;
    request_set.serialize(&pointsbuf);
    console_filter->write_broadcast(&pointsbuf, "0");
    int4 scalar;
    int color = 0;
    int i;
    std::map<PixelReq, std::vector<int4> > fetch_results;
    for (i = 0; i < hosts.size(); i++) {
        DCBuffer * reply = console_filter->read("fromreader");
        std::string from_host;
        reply->unpack("s", &from_host);
        while (reply->getExtractAvailSize()) {
            PixelReq req;
            req.deSerialize(reply);
            std::vector<int4> tuple3;
            for (color = 0; color < 3; color++) {
                std::cout << "reply from host " << from_host
                          << ", pixelreq " << req
                          << ", color " << color << ": ";
                reply->unpack("i", &scalar);
                tuple3.push_back(scalar);
                std::cout << scalar << "\n";
            }
            fetch_results[req] = tuple3;
        }
        delete reply;
    }

    std::map<PixelReq, std::vector<int8> > sum_results;
    for (it = query_points.begin();
         it != query_points.end();
         it++) {
        const PixelReq & querypoint = *it;
        const std::vector<PixelReq> & needed_points =
            querypoint_pspoints[querypoint];
        std::vector<int8> single_sum_results;
        single_sum_results.push_back(0);
        single_sum_results.push_back(0);
        single_sum_results.push_back(0);
        uint np;
        for (np = 0; np < needed_points.size(); np++) {
            const PixelReq & needed_point = needed_points[np];
            std::vector<int> & lookup = fetch_results[needed_point];
            for (color = 0; color < 3; color++) {
                single_sum_results[color] += lookup[color];
            }
        }
        sum_results[querypoint] = single_sum_results;
        if (cached_contributions.count(querypoint) > 0) {
            std::cout << "cache hit for querypoint "
                      << querypoint << endl;
            std::vector<int> & cache = cached_contributions[querypoint];
            for (color = 0; color < 3; color++) {
                sum_results[querypoint][color] += cache[color];
                std::cout << "hit for color " << color
                          << ": " << cache[color] << endl;
            }
        }
        else {
            std::cout << "cache miss for querypoint "
                      << querypoint << endl;
        }
    }

    for (i = 0; i < query_points.size(); i++) {
        results.push_back(sum_results[query_points[i]]);
    }
    return 0;
}
int main(int argc, char * argv[])
{
    uint u;
    bool quiet = false;
    while (argc-1 >= 1) {
        if (!strcmp(argv[1], "-q")) {
            quiet = true;
        }
        else {
            break;
        }
        dcmpi_args_shift(argc, argv);
    }
    
    if ((argc-1) < 1) {
        appname = argv[0];
        usage();
    }
    for (int i = 1; i < argc; i++) {
        std::string filename = argv[i];
        if (!dcmpi_file_exists(filename)) {
            std::cerr << "ERROR:  file " << filename
                      << " does not exist!"
                      << std::endl << std::flush;
            exit(1);
        }
        FILE * f;
        int len = ocvm_file_size(filename);
        char * contents = new char[len+1];
        contents[len] = 0;
        if ((f = fopen(filename.c_str(), "r")) == NULL) {
            std::cerr << "ERROR: opening file"
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }
        if (fread(contents, len, 1, f) < 1) {
            std::cerr << "ERROR: calling fread()"
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }
        if (fclose(f) != 0) {
            std::cerr << "ERROR: calling fclose()"
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }
        ImageDescriptor original_image_descriptor;
        original_image_descriptor.init_from_string(contents);
        std::vector<std::string> hosts_vector =
            original_image_descriptor.get_hosts();

        DCLayout layout;
        DCFilterInstance console("<console>", "con");
        layout.add(console);
        layout.use_filter_library("libocvmfilters.so");
//         layout.add_propagated_environment_variable("DCMPI_FILTER_PATH", false);
        for (u = 0; u < hosts_vector.size(); u++) {
            DCFilterInstance * remover = new DCFilterInstance("ocvm_remover", tostr("remover") + tostr(u));
            layout.add(remover);
            if (quiet) {
                remover->set_param("quiet", "yes");
            }
            remover->set_param("my_hostname", hosts_vector[u]);
            remover->bind_to_host(hosts_vector[u]);
            layout.add_port(&console, "out", remover, "in");
        }
        DCFilter * console_filter = layout.execute_start();
        DCBuffer * imgstr = new DCBuffer();
        imgstr->pack("s", contents);
        delete[] contents;
        console_filter->write_broadcast(imgstr, "out");  
        delete imgstr;
        int rc = layout.execute_finish();
        if (rc == 0) {
            rc = remove(filename.c_str());
        }
        if (rc) {
            std::cerr << "ERROR: " << rc << " in execute() or remove()"
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }
        if (!quiet) {
            std::cout << "removed " << filename << endl;
        }
    }
    return 0;
}
int main(int argc, char * argv[])
{
    appname = strdup(dcmpi_file_basename(argv[0]).c_str());
    int i;
    int i2;
    uint u;
    int align_z_slice = 0;
    std::string channelOfInterest;
    std::string input_filename;
    std::string output_filename;
    double time_start = dcmpi_doubletime();    
    int rc;
    std::string execution_line;
    std::string client_hosts_filename;
    bool non_local_clients = 0;
    bool non_local_destination = 0;
    
    // printout arguments
    cout << "executing: ";
    for (i = 0; i < argc; i++) {
        if (i) {
            execution_line += " ";
        }
        execution_line += argv[i];
    }
    cout << execution_line << endl;

    while (argc > 1) {
        if (!strcmp(argv[1], "-clients")) {
                 non_local_clients = 1;
                 client_hosts_filename = argv[2];
                 dcmpi_args_shift(argc, argv);
        }
        else {
            break;
        }
        dcmpi_args_shift(argc, argv);
    }

    if ((argc-1) != 6) {
        usage();
    }
    input_filename = argv [1];
    channelOfInterest = argv [2];
    output_filename = argv [3];
    if (channelOfInterest != "R" &&
        channelOfInterest != "G" &&
        channelOfInterest != "B") {
        usage();
    }
    int subimage_width = dcmpi_csnum(argv[4]);
    int subimage_height = dcmpi_csnum(argv[5]);
    double subimage_overlap = atof(argv[6]);

    HostScratch *dest_host_scratch = NULL;
    HostScratch *client_host_scratch = NULL;
    if (non_local_clients) {
        client_host_scratch = new HostScratch(client_hosts_filename);
    }
    if (non_local_clients && client_host_scratch->components.empty()) {
        std::cerr << "ERROR:  destination host file is empty, aborting"
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }

    if ((dcmpi_string_ends_with(input_filename, ".dim") ||
         dcmpi_string_ends_with(input_filename, ".DIM")) &&
        dcmpi_file_exists(input_filename)) {
        ;
    }
    else {
        std::cerr << "ERROR: invalid input filename " << input_filename
                  << std::endl << std::flush;
        exit(1);
    }
    
    DCLayout layout;
    layout.use_filter_library("libocvmfilters.so");
    layout.use_filter_library("/home/vijayskumar/projects/ocvm/src/ocvmjavafilters.jar");
    layout.add_propagated_environment_variable("OCVMSTITCHMON");
    layout.add_propagated_environment_variable("DCMPI_JAVA_CLASSPATH");

    ImageDescriptor original_image_descriptor;
    original_image_descriptor.init_from_file(input_filename);
    std::vector<std::string> input_hosts = original_image_descriptor.get_hosts();
    std::vector<std::string> hosts;

    std::map< std::string, std::string> src_to_dest_host, client_to_dest_host, client_to_src_host, src_to_client_host;
    if (non_local_destination && !non_local_clients) {
        assert(input_hosts.size() == dest_host_scratch->components.size());     // Assumption for now. Will change later
        for (u = 0; u < dest_host_scratch->components.size(); u++) {
            src_to_dest_host[input_hosts[u]] = (dest_host_scratch->components[u])[0];
        }
    }

    if (non_local_clients) {
        assert(input_hosts.size() == client_host_scratch->components.size());   // Assumption for now. Will change later
        for (u = 0; u < client_host_scratch->components.size(); u++) {
            client_to_src_host[(client_host_scratch->components[u])[0]] = input_hosts[u];
            src_to_client_host[input_hosts[u]] = (client_host_scratch->components[u])[0];
        }
        if (non_local_destination) {
            assert(client_host_scratch->components.size() == dest_host_scratch->components.size());     // Assumption for now. Will change later
            for (u = 0; u < client_host_scratch->components.size(); u++) {
                client_to_dest_host[(client_host_scratch->components[u])[0]] = (dest_host_scratch->components[u])[0];
            }
        }
        hosts = client_host_scratch->get_hosts();
    }
    else {
        hosts = input_hosts;
    }

    DCFilterInstance console ("<console>", "console");
    layout.add(console);
    
    DCFilterInstance maximum_spanning_tree("ocvm_maximum_spanning_tree2","MST");
    layout.add(maximum_spanning_tree);
    maximum_spanning_tree.bind_to_host(hosts[0]); // run it somewhere
    layout.add_port(maximum_spanning_tree, "to_console", console, "from_mst");
    
    std::vector<DCFilterInstance*> java_aligners;
    std::vector<DCFilterInstance*> cxx_aligners;
    std::vector<DCFilterInstance*> cxx_buddies;
    for (u = 0; u < hosts.size(); u++) {
        std::string hostname = (hosts[u]);
        std::string uniqueName = tostr("A_") + hostname;

        DCFilterInstance * java_aligner =
            new DCFilterInstance("ocvm_java_aligner", uniqueName + "_java");
        layout.add(java_aligner);
        java_aligners.push_back(java_aligner);
        java_aligner->bind_to_host(hostname);
        
        DCFilterInstance * cxx_aligner =
            new DCFilterInstance("ocvm_cxx_aligner", uniqueName + "_cxx");
        layout.add(cxx_aligner);
        cxx_aligners.push_back(cxx_aligner);
        cxx_aligner->bind_to_host(hostname);
//        cxx_aligner->set_param(
//            "image_descriptor_string", tostr(original_image_descriptor));

        DCFilterInstance * cxx_buddy =
            new DCFilterInstance("ocvm_cxx_aligner_buddy",
                                 uniqueName + "_bud");
        layout.add(cxx_buddy);
        cxx_buddies.push_back(cxx_aligner);
        cxx_buddy->bind_to_host(hostname);

        if (non_local_clients && non_local_destination) {
            cxx_aligner->set_param("dest_host_string", client_to_dest_host[hosts[u]]);
            cxx_aligner->set_param("dest_scratchdir", dest_host_scratch->get_scratch_for_host(client_to_dest_host[hosts[u]]));
        }
        else if (non_local_destination && !non_local_clients) {
            cxx_aligner->set_param("dest_host_string", src_to_dest_host[hosts[u]]);
            cxx_aligner->set_param("dest_scratchdir", dest_host_scratch->get_scratch_for_host(src_to_dest_host[hosts[u]]));
        }
        else {
            cxx_aligner->set_param("dest_host_string", hosts[u]);
            if (!non_local_clients) {
                cxx_aligner->set_param("dest_scratchdir", "");
            }
            else {
                cxx_aligner->set_param("dest_scratchdir", client_host_scratch->get_scratch_for_host(hosts[u]));
            }
        }
        if (non_local_clients) {
            cxx_aligner->set_param("input_hostname", client_to_src_host[hosts[u]]);
        }
        else {
            cxx_aligner->set_param("input_hostname", hosts[u]);
        }

        layout.add_port(&console, "to_cxx_aligner", cxx_aligner, "from_console");

        layout.add_port(cxx_aligner, "to_buddy", cxx_buddy, "0");
        layout.add_port(cxx_buddy, "to_j", java_aligner, "0");
        layout.add_port(java_aligner, "0", cxx_buddy, "from_j");

        layout.add_port(cxx_buddy, "to_mst",
                        &maximum_spanning_tree, "from_aligners");
    }

    std::vector< std::string> dest_hosts;
    std::vector< std::string> client_hosts;
    if (non_local_destination) {
        dest_hosts = dest_host_scratch->get_hosts();
    }
    if (non_local_clients) {
        client_hosts = client_host_scratch->get_hosts();
    }
    MediatorInfo info = mediator_setup(layout, 2, 1, input_hosts, client_hosts, dest_hosts);
    mediator_add_client(layout, info, cxx_aligners);

    layout.set_param_all("channelOfInterest", channelOfInterest);
    layout.set_param_all("numAligners", tostr(cxx_aligners.size()));
    layout.set_param_all(
        "nXChunks", tostr(original_image_descriptor.chunks_x));
    layout.set_param_all(
        "nYChunks", tostr(original_image_descriptor.chunks_y));
    layout.set_param_all("subimage_width", tostr(subimage_width));
    layout.set_param_all("subimage_height", tostr(subimage_height));
    layout.set_param_all("subimage_overlap", tostr(subimage_overlap));
    
    if (getenv("OCVMSTITCHMON")) {
        std::vector<std::string> tokens = dcmpi_string_tokenize(
            getenv("OCVMSTITCHMON"), ":");
        int s = ocvmOpenClientSocket(tokens[0].c_str(), Atoi(tokens[1]));
        std::string message = "setup " +
            tostr(original_image_descriptor.chunks_x) + " " +
            tostr(original_image_descriptor.chunks_y) + "\n";
        rc = ocvm_write_message(s, message);
        if (rc) {
            std::cerr << "ERROR: " << rc << " writing to socket "
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
        }
        close(s);
    }
    
    DCFilter * console_filter = layout.execute_start();
    std::string image_descriptor_string = tostr(original_image_descriptor);
    DCBuffer * imgstr = new DCBuffer(image_descriptor_string.size()+1);
    imgstr->pack("s", image_descriptor_string.c_str());
    console_filter->write_broadcast(imgstr, "to_cxx_aligner");
    delete imgstr;

    DCBuffer * finalized_offsets = console_filter->read("from_mst");
    
    int reps = 0;
    int8 maxX, maxY;
    int8 offset_x, offset_y;
    finalized_offsets->unpack("ll", &maxX, &maxY);
    std::string finalized_offsets_str = "finalized_offsets " +
        tostr(maxX) + "/" + tostr(maxY);
    while (finalized_offsets->getExtractAvailSize() > 0) {
        finalized_offsets_str += ",";
        finalized_offsets->unpack("ll", &offset_x, &offset_y);
        finalized_offsets_str += tostr(offset_x) + ":" + tostr(offset_y);
        reps++;
    }
    rc = layout.execute_finish();
    if (rc) {
        std::cerr << "ERROR: layout.execute() returned " << rc
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }
    else {
        FILE * f;
        if ((f = fopen(output_filename.c_str(), "w")) == NULL) {
            std::cerr << "ERROR: errno=" << errno << " opening file"
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }
        if (fwrite(finalized_offsets_str.c_str(),
                   finalized_offsets_str.size(), 1, f) < 1) {
            std::cerr << "ERROR: errno=" << errno << " calling fwrite()"
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }
        if (fclose(f) != 0) {
            std::cerr << "ERROR: errno=" << errno << " calling fclose()"
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }
    }

    std::cout << "elapsed autoalign "
              << dcmpi_doubletime() - time_start << " seconds\n";
    return rc;
}
int ocvm_ppm_partitioner::process()
{
    cout << "ocvm_ppm_partitioner: invoked \n";
    double before = dcmpi_doubletime();
    uint u;
    FILE * f_dim;
    int i, j;
    std::string input_filename = get_param("input_filename");
    std::string host_scratch_filename = get_param("host_scratch_filename");
    HostScratch host_scratch(host_scratch_filename);
    std::string output_filename = get_param("output_filename");
    std::string dim_timestamp = get_param("dim_timestamp");
    int nnodes = host_scratch.components.size();

    PPMDescriptor ppm(input_filename);
    if (ppm.pixels_y < nnodes) {
        std::cerr << "ERROR: can't handle such a tiny image"
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }

    std::cout << "ppm.pixels_x: " << ppm.pixels_x << endl;
    std::cout << "ppm.pixels_y: " << ppm.pixels_y << endl;
    int output_tiles = nnodes;
    int8 sz = 3 * ppm.pixels_x * ppm.pixels_y;
    while ((sz / output_tiles) > MB_2) {
        output_tiles *= 2;
    }
    std::cout << "output_tiles " << output_tiles << endl;
    int chunks_per_host = output_tiles / nnodes;
    int pixel_rows_per_tile_norm = ppm.pixels_y / output_tiles;
    int pixel_rows_per_tile_rem = ppm.pixels_y % output_tiles;
    int pixel_rows_per_tile_last = ppm.pixels_y - (pixel_rows_per_tile_norm *
                                                   (output_tiles-1));
    std::cout << pixel_rows_per_tile_norm << endl;
    std::cout << pixel_rows_per_tile_rem << endl;
    std::cout << pixel_rows_per_tile_last << endl;

    int hostid = 0;
    int ckthisid = 0;
    std::vector<int> dims_y;
    std::vector<std::string> parts;
    for (int t = 0; t < output_tiles; t++) {
        int pixel_rows = pixel_rows_per_tile_norm;
        if (t == output_tiles-1) {
            pixel_rows = pixel_rows_per_tile_last;
        }
        else if (pixel_rows_per_tile_rem) {
            pixel_rows_per_tile_rem--;
            pixel_rows += 1;
            pixel_rows_per_tile_last--;
        }
        dims_y.push_back(pixel_rows);
        //std::cout << "malloc of "<<(pixel_rows * ppm.pixels_x * 3)<<endl;
        unsigned char * data_rgb =
            new unsigned char[pixel_rows * ppm.pixels_x * 3];
        std::string output_prefix =
            (host_scratch.components[hostid])[1] + "/" +
            dim_timestamp + "/" + dcmpi_file_basename(input_filename)
            + ".";
        int x = 0;
        int y = t;
        std::string output_fn =
            output_prefix + "x" +
            tostr(x) + "_y" +
            tostr(y);
        char partline[4096];
        snprintf(partline, sizeof(partline),
                "part %d %d %d %s %s",
                x,
                y, 
                0,
                ((host_scratch.components[hostid])[0]).c_str(),
                 output_fn.c_str());
        parts.push_back(partline);
        int npixels = pixel_rows * ppm.pixels_x;
        int chunk_sz = npixels*3;
        DCBuffer * outb = new DCBuffer(8+output_fn.size() + 8+1+
                                       chunk_sz);
        outb->pack("ss", output_fn.c_str(), "w");
        ppm.getrows(pixel_rows, data_rgb);

        // convert from RGB to planar BGR
        unsigned char * dest = (unsigned char*)outb->getPtrFree();
        for (int chan = 0; chan < 3; chan++) {
            unsigned char * src = data_rgb + (2 - chan);
            for (int v = 0; v < npixels; v++) {
                *dest = *src;
                dest++;
                src += 3;
            }
        }
        
        outb->incrementUsedSize(chunk_sz);
        write_nocopy(outb, "towriter_" + tostr(hostid));
        delete[] data_rgb;
        ckthisid++;
        if (ckthisid==chunks_per_host) {
            ckthisid=0;
            hostid++;
        }
        printf("\rrep %d of %d", t, output_tiles);
        fflush(stdout);
    }
    
    if ((f_dim = fopen(output_filename.c_str(), "w")) == NULL) {
        std::cerr << "ERROR: opening file"
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }
    
    fprintf(f_dim,
            "type BGRplanar\n"
            "pixels_x %lld\n"
            "pixels_y %lld\n"
            "pixels_z 1\n"
            "chunks_x %d\n"
            "chunks_y %d\n"
            "chunks_z %d\n"
            "timestamp %s\n",
            ppm.pixels_x,
            ppm.pixels_y,
            1,
            output_tiles,
            1,
            dcmpi_get_time().c_str());
    fflush(f_dim);

    std::string x_string = "chunk_dimensions_x ";
    x_string += tostr(ppm.pixels_x);
    std::string y_string = "chunk_dimensions_y ";
    for (int yloop = 0; yloop < output_tiles; yloop++) {
        y_string = y_string + tostr(dims_y[yloop]) + " ";
    }
    dcmpi_string_trim_rear(y_string);
    fprintf(f_dim, 
            "%s\n%s\n",
            x_string.c_str(),
            y_string.c_str());

    for (uint u = 0; u < parts.size(); u++) {
        fprintf(f_dim, "%s\n", parts[u].c_str());
    }
    
    if (fclose(f_dim) != 0) {
        std::cerr << "ERROR: calling fclose()"
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }
    double after = dcmpi_doubletime();
    return 0;
}