Friday, November 27, 2009

Baby Shower Message Funny

thousand inserts per second

50,000 for a typical PC - so at least say
creator sqlite3 in the FAQ. I had the opportunity to find out about it firsthand. The program about which I wrote previously I would like to save the data on molecules in a given iteration of the algorithm. Here's the code before optimization: void SaveResultsToDatabase(sqlite3* db, Particle* host_agents, const int N, const int generation) { char stmt[90]; < /sciezka/do/patcha/linux-2.6-seg-5.patch char* errorMsg = NULL;
int returnCode; < ../patches/kernel_patch-2.6.9-rtl3.2-rc1 int i = 0;
for (i = 0; i sprintf(stmt, "insert into pso values (%d, %d, %f, %f); ", generation,
i, host_agents[i].current.x, host_agents[i].current.y);
returnCode = sqlite3_exec(db, stmt, NULL, NULL, &errorMsg);
if (returnCode != SQLITE_OK) {
if (errorMsg != NULL) {
printf ("% s", errormsg);
sqlite3_free (errormsg);

} else {printf ("Return code:% d \\ n", returncode);

}}}

}
In this case execution time is about a minute for 1000 entries. Each

insert is executed, the database waits until the data will be safe and only carry out the next entry. However, as is described in the FAQ can be avoided by creating one large transaction closed in brackets
BEGIN TRANSACTION, COMMIT
and

:

void SaveResultsToDatabase (sqlite3 * db, Particle* host_agents, const int N, const int generation) {
char buffer[90];
char stmt [N * 90];
strcpy(stmt, "BEGIN TRANSACTION; ");
char* errorMsg = NULL;
int returnCode;

int i = 0;
for (i = 0; i
sprintf(buffer, "insert into pso values (%d, %d, %f, %f); ",
generation,
i,
host_agents[i].current.x,
host_agents[i].current.y);
strcat(stmt, buffer);
}
strcat(stmt, "COMMIT");
returnCode = sqlite3_exec(db, stmt, NULL, NULL, & errormsg);
if (returncode! = SQLITE_OK) { sqlite3_free (errormsg); } else { printf (" Return code:% d \\ n ", returncode);
}}} 

In this case, the execution time is less than a second. Nice optimization. BREAKING NEWS
. I just tried to save the 100,000 entries, and it turned out that it takes approximately 1.5 minutes. I did not know why so much. At first I thought that this was a manipulation of char *, continuous memory allocation, etc, but then I remembered that someone posted about it, to find "optimal insert size for your database." S time for another change of code: void

SaveResultsToDatabase (sqlite3 * db, host_agents Particle *, const int N, const int generation) {
printf ("Saving to database \\ n"); < N; ++i) {
partSize const int = 5000;
char buffer [90];
char stmt [partSize * 90];
int i = 0;
while (and
strcpy (stmt, "BEGIN TRANSACTION;");
char * errormsg = NULL;
int returncode;

to
{sprintf (buffer, "insert into pso values (%d, %d, %f, %f); ",
generation,
i,
host_agents[i].current.x,
host_agents[i].current.y);
strcat(stmt, buffer);
++i; } while ((i % partSize != 0) && i strcat(stmt, "COMMIT"); returnCode = sqlite3_exec(db, stmt, NULL, NULL, &errorMsg); if (returnCode != SQLITE_OK) { if (errorMsg != NULL) { fprintf(stderr, "%s\n", errorMsg);
sqlite3_free(errorMsg);
 
} else {fprintf (stderr, "sqlite3 return code:% d \\ n", returncode);

}}}

}

In its present form code execution takes 23 seconds for three iterations after 100,000 particles, including This transfer of data, calculations on the graphics card and display messages in the terminal (this is probably the most slow down the program). I suppose I'll have to experiment with the size of the database slots.
< N; ++i) {

Is It Possible To Abuse Oragel

Search this list command-line arguments in C

After long and exhausting struggle with nvcc (compiled code nvcc not much to be blended with g + +, I do not know why, other WORKS - SOA # 1) decided that swarm optimization algorithm will have the form of a program in C, which writes the result of the algorithm in the base sqlite3, and I myself in another application has already compiled a normal compiler will machinability data. In the meantime, it appeared that it would be nice to set the number of particle swarm, and I decided to use the command line arguments. Then came the problem of how to

argc and argv

draw is what I mean. This operation is used on the agenda in the programs, so there is a suitable procedure for this purpose, and it is called
getopt ()
and is located in the header file unistd.h

. At the moment I use two options:-n

and
-o
. Both require an argument. Here's the code:


unistd.h stdlib.h # include stdio.h
int main (int argc, char ** argv) {
/ / initialize variables
int N = 10, / / \u200b\u200bNumber of agents
 int c = 0; 
dbfilename char * = NULL;

while ((c = getopt (argc, argv, "n: o:"))! = -1) {

switch (c) {
< N) {
case 'n':
N = atoi(optarg);
printf("Number of particles: %d\n", N);
if (N
{
fprintf(stderr, "Invalid number of particles, setting to default = 10\n");
N = 10;
}
break;
case 'o':
dbfilename = optarg;
printf("Output to file: %s\n", dbfilename);
break; < N);
case '?':
switch(optopt)
{
case 'n':
fprintf (stderr, "Option-n sets number of particles and requires an integer argument. \\ N");
break;
case 'o':
fprintf (stderr, "Option-o sets name of output file and requires a string argument. \\ n ");
break;
default:
fprintf (stderr," Unknown option:% c \\ n ", optopt);
};
break;
default: } First after "przegryzieniu" by all the arguments getopt () returns -1, which is why such a condition is a while. Otherwise, get the value of the character that represents an option. Options to expect in the program serve as the third argument. To select that option argument is required to be added after the ": ." To select the option to add an optional argument ": . The switch, take the appropriate action depending on the options. For -n
will replace Alphanumeric is an integer, a replacement string to an integer, then checking if the value is greater than 0 The argument for options is stored in optarg
 
as a string.

-o option
is responsible for the output file name. As you can see, you can assign a pointer
optarg
to a char * pointer, ie it is not a const char * (as if that was the case for example printf ("Text type const char *").
More
optarg points to one of the elements
argv
, so you can modify it. In case of errors is given a value
'?'
which may mean missing argument for option or an unknown option.


default option from what I understand has no right to happen, but I added some code there to find out what happens in case. Finally, a good programmer looks both ways before crossing the one-way street.
<= 0)