Exemplo n.º 1
0
void Program::build(const std::string& options)
{
  bool createdProgramsFromSource = false;
  if (_clPrograms.empty()) {
    createProgramsFromSource();
    createdProgramsFromSource = true;
  }

  try {
    // build program for each device
    // TODO: how to build the program only for a subset of devices?
    for (auto& devicePtr : globalDeviceList) {
      _clPrograms[devicePtr->id()].build(
            std::vector<cl::Device>(1, devicePtr->clDevice()),
            options.c_str() );
    }

    if (createdProgramsFromSource) {
      saveBinary();
    }

    if (util::envVarValue("SKELCL_PRINT_BUILD_LOG") == "YES") {
      printBuildLog();
    }

  } catch (cl::Error& err) {
    if (err.err() == CL_BUILD_PROGRAM_FAILURE) {
      LOG_ERROR(err);
      printBuildLog();
      ABORT_WITH_ERROR(err);
    } else {
      ABORT_WITH_ERROR(err);
    }
  }
}
Exemplo n.º 2
0
std::pair<size_t, size_t> FlashLibraryItem::saveBinaryContainer(const FlashString& id, std::vector<unsigned char>& buffer){
	size_t where = beginWriteBinary(id, buffer);
	size_t whereSymbolStarts = buffer.size();
	saveBinary(id, buffer);
	endWriteBinary(buffer, where);
	return std::make_pair(whereSymbolStarts, buffer.size() - whereSymbolStarts);
}
Exemplo n.º 3
0
	bool saveProgram(GLuint ProgramName, std::string const & String)
	{
		GLint Size(0);
		GLenum Format(0);

		glGetProgramiv(ProgramName, GL_PROGRAM_BINARY_LENGTH, &Size);
		std::vector<glm::byte> Data(Size);
		glGetProgramBinary(ProgramName, Size, nullptr, &Format, &Data[0]);
		saveBinary(String, Format, Data, Size);

		return this->checkError("saveProgram");
	}
Exemplo n.º 4
0
int LoadList::saveBinary(const char * filename, int numListEntries, int * listEntries, int offset)
{
  FILE * fout;
  fout = fopen(filename, "wb");
  if (!fout)
  {
    printf("Error: could not open list file %s.\n",filename);
    return 1;
  }

  int code = saveBinary(fout, numListEntries, listEntries, offset);
  fclose(fout);
  return code;
}
Exemplo n.º 5
0
	///////////////////////////////////////////////////
	// operate the message
	void operator() (MsgPair& m) {
		if (m.second.isACK()) {
			if (m.second.isBinary())
				ch.log("Binary message ["+ m.second.fileName() +"] is acknowledged by receiver!");
			else
				ch.log("String message is acknowledged by receiver!");
		}
		else if (m.second.isBinary()) {
			std::string path = saveBinary(m.second);
			ch.log("File ["+ m.second.fileName() +"] is received and saved to ["+ path +"]!");
		}
		else {
			std::string str = saveString(m.second);
			ch.log("String ["+ str +"] is received!");
			conductTask(m);	// see if there is any work to do
		}
	}
Exemplo n.º 6
0
int LoadList::saveBinaryMulti(const char * filename, int numLists, int * numListEntries, int ** listEntries, int offset)
{
  FILE * fout;
  fout = fopen(filename, "wb");
  if (!fout)
  {
    printf("Error: could not open list file %s.\n",filename);
    return 1;
  }

  fwrite(&numLists, sizeof(int), 1, fout);

  int code = 0;
  for(int i=0; i<numLists; i++)
    code = code || saveBinary(fout, numListEntries[i], listEntries[i], offset);

  fclose(fout);

  return code;
}
Exemplo n.º 7
0
int main(int argc, char **argv)
{
    FILE *fp;
    int width, height, alpha;
    unsigned char *image, *fb;
    SDL_Surface *screen;
    struct triangles *triangles, *best, *absbest;
    long long diff;
    float percdiff, bestdiff;

    /* Initialization */
    srand(time(NULL));
    state.max_shapes = 64;
    state.max_shapes_incremental = 1;
    state.temperature = 0.10;
    state.generation = 0;
    state.absbestdiff = 100; /* 100% is worst diff possible. */

    /* Check arity and parse additional args if any. */
    if (argc < 4) {
        showHelp(argv[0]);
        exit(1);
    }

    if (argc > 4) {
        int j;
        for (j = 4; j < argc; j++) {
            int moreargs = j+1 < argc;

            if (!strcmp(argv[j],"--use-triangles") && moreargs) {
                opt_use_triangles = atoi(argv[++j]);
            } else if (!strcmp(argv[j],"--use-circles") && moreargs) {
                opt_use_circles = atoi(argv[++j]);
            } else if (!strcmp(argv[j],"--max-shapes") && moreargs) {
                state.max_shapes = atoi(argv[++j]);
            } else if (!strcmp(argv[j],"--initial-shapes") && moreargs) {
                state.max_shapes_incremental = atoi(argv[++j]);
            } else if (!strcmp(argv[j],"--mutation-rate") && moreargs) {
                opt_mutation_rate = atoi(argv[++j]);
            } else if (!strcmp(argv[j],"--restart")) {
                opt_restart = 1;
            } else if (!strcmp(argv[j],"--help")) {
                showHelp(argv[0]);
            } else {
                fprintf(stderr,"Invalid options.");
                showHelp(argv[0]);
                exit(1);
            }
        }
    }

    /* Sanity check. */
    if (state.max_shapes_incremental > state.max_shapes)
        state.max_shapes = state.max_shapes_incremental;
    if (opt_mutation_rate > 1000)
        opt_mutation_rate = 1000;

    /* Load the PNG in memory. */
    fp = fopen(argv[1],"rb");
    if (!fp) {
        perror("Opening PNG file");
        exit(1);
    }
    if ((image = PngLoad(fp,&width,&height,&alpha)) == NULL) {
        printf("Can't load the specified image.");
        exit(1);
    }

    printf("Image %d %d, alpha:%d at %p\n", width, height, alpha, image);
    fclose(fp);

    /* Initialize SDL and allocate our arrays of triangles. */
    screen = sdlInit(width,height,0);
    fb = malloc(width*height*3);
    triangles = mkRandomtriangles(state.max_shapes,width,height);
    best = mkRandomtriangles(state.max_shapes,width,height);
    absbest = mkRandomtriangles(state.max_shapes,width,height);
    state.absbestdiff = bestdiff = 100;

    /* Load the binary file if any. */
    if (!opt_restart) {
        loadBinary(argv[2],best);
    } else {
        best->inuse = state.max_shapes_incremental;
    }
    absbest->inuse = best->inuse;
    memcpy(absbest->triangles,best->triangles,
        sizeof(struct triangle)*best->count);

    /* Show the current evolved image and the real image for one scond each. */
    memset(fb,0,width*height*3);
    drawtriangles(fb,width,height,best);
    sdlShowRgb(screen,fb,width,height);
    sleep(1);
    sdlShowRgb(screen,image,width,height);
    sleep(1);

    /* Evolve the current solution using simulated annealing. */
    while(1) {
        state.generation++;
        if (state.temperature > 0 && !(state.generation % 10)) {
            state.temperature -= 0.00001;
            if (state.temperature < 0) state.temperature = 0;
        }

        /* From time to time allow the current solution to use one more
         * triangle, up to the configured max number. */
        if ((state.generation % 1000) == 0) {
            if (state.max_shapes_incremental < triangles->count &&
                triangles->inuse > state.max_shapes_incremental-1)
            {
                state.max_shapes_incremental++;
            }
        }

        /* Copy what is currenly the best solution, and mutate it. */
        memcpy(triangles->triangles,best->triangles,
            sizeof(struct triangle)*best->count);
        triangles->inuse = best->inuse;
        mutatetriangles(triangles,10,width,height);

        /* Draw the mutated solution, and check what is its fitness.
         * In our case the fitness is the difference bewteen the target
         * image and our image. */
        memset(fb,0,width*height*3);
        drawtriangles(fb,width,height,triangles);
        diff = computeDiff(image,fb,width,height);

        /* The percentage of difference is calculate taking the ratio between
         * the maximum difference and the current difference.
         * The magic constant 422 is actually the max difference between
         * two pixels as r,g,b coordinates in the space, so sqrt(255^2*3). */
        percdiff = (float)diff/(width*height*442)*100;
        if (percdiff < bestdiff ||
            (state.temperature > 0 &&
             ((float)rand()/RAND_MAX) < state.temperature &&
             (percdiff-state.absbestdiff) < 2*state.temperature))
        {
            /* Save what is currently our "best" solution, even if actually
             * this may be a jump backward depending on the temperature.
             * It will be used as a base of the next iteration. */
            best->inuse = triangles->inuse;
            memcpy(best->triangles,triangles->triangles,
                sizeof(struct triangle)*best->count);

            if (percdiff < bestdiff) {
                /* We always save a copy of the absolute best solution we found
                 * so far, after some generation without finding anything better
                 * we may jump back to that solution.
                 *
                 * We also use the absolute best solution to save the program
                 * state in the binary file, and as SVG output. */
                absbest->inuse = best->inuse;
                memcpy(absbest->triangles,best->triangles,
                    sizeof(struct triangle)*best->count);
                state.absbestdiff = percdiff;
            }

            printf("Diff is %f%% (inuse:%d, max:%d, gen:%lld, temp:%f)\n",
                percdiff,
                triangles->inuse,
                state.max_shapes_incremental,
                state.generation,
                state.temperature);

            bestdiff = percdiff;
            sdlShowRgb(screen,fb,width,height);
        }
        processSdlEvents();

        /* From time to time save the current state into a binary save
         * and produce an SVG of the current solution. */
        if ((state.generation % 100) == 0) {
            saveSvg(argv[3],absbest,width,height);
            saveBinary(argv[2],absbest);
        }
    }
    return 0;
}
Exemplo n.º 8
0
bool
LadderList::save()
{
  sortAndUpdate();
  return saveBinary();
}
Exemplo n.º 9
0
//--------------------
//基本テスト
void basic_test()
{
	std::printf("\n");
	std::printf("================================================================================\n");
	std::printf("[ Test for serialization basic ]\n");
	std::printf("\n");

	//テストデータ作成
	std::printf("----------------------------------------\n");
	std::printf("[ Make data ]\n");
	basicTestData data;
	data.m_memberA = 123;
	data.m_memberB = 4.56f;
	data.m_memberC[0] = 78;
	data.m_memberC[1] = 90;
	data.m_memberD.m_item1 = 987;
	data.m_memberD.m_item2 = 654;

	//テストデータの内容表示
	std::printf("----------------------------------------\n");
	std::printf("[ Print data(1) : before save ]\n");
	auto print_data = [](basicTestData& data)
	{
		std::printf("basicTestData\n");
		std::printf("  .m_memberA = %d\n", data.m_memberA);
		std::printf("  .m_memberB = %.2f\n", data.m_memberB);
		std::printf("  .m_memberC = { %d, %d }\n", data.m_memberC[0], data.m_memberC[1]);
		std::printf("  .m_memberD\n");
		std::printf("      .m_item1 = %d\n", data.m_memberD.m_item1);
		std::printf("      .m_item2 = %d\n", data.m_memberD.m_item2);
	};
	print_data(data);
	
	//バイナリ形式セーブデータをセーブ
	std::printf("----------------------------------------\n");
	std::printf("[ Save binary ]\n");
	saveBinary(data);

	//テキスト形式セーブデータをセーブ
	std::printf("----------------------------------------\n");
	std::printf("[ Save text ]\n");
	saveText(data);

	//一旦データをクリア
	std::memset(&data, 0, sizeof(data));

	//テストデータの内容表示
	std::printf("----------------------------------------\n");
	std::printf("[ Print data(2) : before load ]\n");
	print_data(data);

	//バイナリ形式セーブデータをロード
	std::printf("----------------------------------------\n");
	std::printf("[ Load binary ]\n");
	loadBinary(data);

#if 0//未実装
	//テキスト形式セーブデータをロード
	std::printf("----------------------------------------\n");
	std::printf("[ Load text ]\n");
	loadText(data);
#endif
	
	//テストデータの内容表示
	std::printf("----------------------------------------\n");
	std::printf("[ Print data(3) : after load ]\n");
	print_data(data);

	std::printf("\n");
	std::printf("================================================================================\n");
	std::printf("finish.\n");
}