void sequentialJulia()
{
	int i, j, maxk=0;
	float  y0;
	double  t1, t2, scale;
	scale = ((float)colors)/(log((float)MAX_ITER));
  pthread_t th[TH];
  params p[TH];
	t1 = get_time_sec();
	for (i = 0; i < num_rows; i++)
	{
		y0 = y_stop - ((float)i)*y_delta; 
    for(j=0;j<TH;j++){
      //start threads:
      p[j].id=j;
      p[j].scale=scale;
      p[j].y0=y0;
      p[j].row=i;
      pthread_create(&th[j],NULL,column_par,(void*)&p[j]);
    }
	} // loop over rows
  for(i=0;i<TH;i++){
    pthread_join(th[i],NULL);
  }
	t2 = get_time_sec();
    /* write out the resulting image */
    writePPM(image, num_rows, num_cols, colors /* s/b 255 */, "julia_seq.ppm");
	printf("Sequential code ran in %f seconds\n",t2-t1);
	printf("max k is %i\n",maxk);
} // end sequentialJulia
Beispiel #2
0
void limit_rate(session_t* sess, int bytes_transfered, int is_upload)
{
	//transfering the data
	sess->data_process = 1;

	long curr_sec = get_time_sec();
	long curr_usec = get_time_usec();

	double elapsed;
	elapsed = curr_sec - sess->bw_transfer_start_sec;
	elapsed += (double)(curr_usec - sess->bw_transfer_start_usec) / (double)1000000;

	if(elapsed <= 0.0)
		elapsed = 0.01;

	//curr transfer speed
	unsigned int bw_rate = (unsigned int)((double)bytes_transfered / elapsed);
	
	double rate_ratio;
	if(is_upload)
	{
		if(bw_rate <= sess->bw_upload_rate_max)
		{
			sess->bw_transfer_start_sec = get_time_sec();
        		sess->bw_transfer_start_usec = get_time_usec();
			return;
		}
		rate_ratio = bw_rate / sess->bw_upload_rate_max;
	}
	else
	{
		if(bw_rate <= sess->bw_download_rate_max)
		{
			sess->bw_transfer_start_sec = get_time_sec();
        		sess->bw_transfer_start_usec = get_time_usec();
			return;
		}
		rate_ratio = bw_rate / sess->bw_download_rate_max;	
	}

	double pause_time;
	pause_time = (rate_ratio - 1.0)	* elapsed;
	nano_sleep(pause_time);

	sess->bw_transfer_start_sec = get_time_sec();
	sess->bw_transfer_start_usec = get_time_usec();
}
/* Main routine. */
int main(int argc, char* argv[]) 
{
  int i;
  int count = 0;
  double t1, t2;
  t1 = get_time_sec();
  for (i=1; i<=FINAL_NUM; i++) {
    if (is_prime(i)) {
      //printf("%d: %d is prime\n",id,i);
      count++;
    }
  }
  t2 = get_time_sec();
  int denom = FINAL_NUM;
  printf("There are %d prime numbers <= %d\n",count,FINAL_NUM);
  printf("That is a ratio of %f, which should be about %f\n",
         (double)count/(double)denom,
         1.0/log((double)denom));
         
  printf("It took %f seconds for the whole thing to run\n",t2-t1); 
	
  return 0;
} // end main
Beispiel #4
0
/**
 ****************************************************************************************
 * @brief get time with year,month,day,hour,minute,second
 ****************************************************************************************
 */
void qn_time_get(qn_tm_t *ptm)
{
    struct tm *ltime;
    const time_t sec = get_time_sec();

    // function inside memery manage, and should use gmtime() function but there have a problem
    ltime = localtime(&sec);

    ptm->seconds = ltime->tm_sec;
    ptm->minutes = ltime->tm_min;
    ptm->hour    = ltime->tm_hour;
    ptm->day     = ltime->tm_mday;
    ptm->month   = ltime->tm_mon + 1;
    ptm->year    = ltime->tm_year + 1900;
}
int main(int argc, char* argv[]) {
  int numFish;
  int initMode;
  int numFrames;
  float r, elongation, polarization;
  
  if (argc < 5) {
    printf("Usage: sim_fast <N> <init> <nframes> <r>\n");
    printf("  where\n");
    printf("       <N> is the number of fish\n");
    printf("       <init> determines the initial configuration:\n");
    printf("          0 - random with clock as seed\n");
    printf("          1 - random with seed 1\n");
    printf("          2 - evenly spaced, aligned N, S, E, or W\n");
    printf("       <nframes> is the number of frames to simulate\n");
    printf("       <r> is the ratio of orientation to attraction\n");
    return;
  }

  // handle input
  numFish = atoi(argv[1]);
  initMode = atoi(argv[2]);
  numFrames = atoi(argv[3]);
  r = (float)atof(argv[4]);

  // Create the ocean
  float *Px = malloc(sizeof(float)*numFish);
  float *Py = malloc(sizeof(float)*numFish);
  float *Vx = malloc(sizeof(float)*numFish);
  float *Vy = malloc(sizeof(float)*numFish);
  switch( initMode ) {
    case 0:
      srand(time(NULL));
      fillRandomOcean(Px,Py,Vx,Vy,numFish);
      break;
    case 1:
      fillSemiRandomOcean(Px,Py,Vx,Vy,numFish);
      break;
    case 2:
      fillRegularOcean(Px,Py,Vx,Vy,numFish);
      break;
    default:
      return;
  }

  double t1, t2;
  t1 = get_time_sec();

  // Do all the work, here!
  runSimulationWithStatistics( Px, Py, Vx, Vy, numFish, r, numFrames, &elongation, &polarization);

  t2 = get_time_sec();
  printf("It took %f seconds to simulate and compute statistics\n",t2-t1);
  printf("Elongation is %f, polarization is %f\n",elongation,polarization);

  // clean up
  free(Px);
  free(Py);
  free(Vx);
  free(Vy);
} // end main
Beispiel #6
0
static void do_retr(session_t *sess)
{
	if(get_transfer_fd(sess) == 0)
                return;
	
	int fd = open(sess->arg, O_RDONLY);
	if(fd == -1)
	{
		ftp_reply(sess, FTP_FILEFAIL, "1Failed to open file.");
		return;
	}

	int ret;
	ret = lock_file_read(fd);	
	if(ret == -1)
	{
		ftp_reply(sess, FTP_FILEFAIL, "Failed to open file.");
                return;		
	}

	//device file can not be downloaded
	struct stat sbuf;
	ret = fstat(fd, &sbuf);
	if(!S_ISREG(sbuf.st_mode))
	{
		ftp_reply(sess, FTP_FILEFAIL, "Failed to open file.");
                return;
	}
	
	long long offset = sess->restart_pos;
        sess->restart_pos = 0;

	if(offset != 0)
	{
		ret = lseek(fd, offset, SEEK_SET);
		if(ret == -1)
		{
			ftp_reply(sess, FTP_FILEFAIL, "Failed to open file.");
	                return;
		}
	}

	char text[1024] = {0};
	if(sess->is_ascii)
	{
		sprintf(text, "Opening ASCII mode data conection for %s (%lld bytes).",
                sess->arg, (long long)sbuf.st_size);
	}
	else
	{
		sprintf(text, "Opening BINARY mode data conection for %s (%lld bytes).",
		sess->arg, (long long)sbuf.st_size);
	}	

        ftp_reply(sess, FTP_DATACONN, text);

	int flag = 0;
	long long bytes_to_send = sbuf.st_size;

	if(offset > bytes_to_send)
		bytes_to_send = 0;
	else
		bytes_to_send -= offset;

	sess->bw_transfer_start_sec = get_time_sec();
	sess->bw_transfer_start_usec = get_time_usec();

	while(bytes_to_send)
	{
		int num_this_time = bytes_to_send > 65536 ? 65536 : bytes_to_send;
		ret = sendfile(sess->data_fd, fd, NULL, num_this_time);
		if(ret == -1)
		{
			flag = 2;
			break;
		}
		
		limit_rate(sess, ret, 0);
		if(sess->abor_received)
		{
			flag = 2;
			break;
		}	

		bytes_to_send -= ret;
	}

        close(sess->data_fd);
        sess->data_fd = -1;
	close(fd);

	if(flag == 0)
        	ftp_reply(sess, FTP_TRANSFEROK, "Transfer complete.");

	else if(flag == 1)
		ftp_reply(sess, FTP_BADSENDFILE, "Failure reading from local file.");

	else if(flag == 2)
		ftp_reply(sess, FTP_BADSENDNET, "Failure writing to network stream.");

	check_abor(sess);
	start_cmdio_alarm();
}
Beispiel #7
0
void upload_common(session_t* sess, int is_append)
{
	if(get_transfer_fd(sess) == 0)
                return;

        int fd = open(sess->arg, O_CREAT | O_WRONLY, 0666);
        if(fd == -1)
        {
                ftp_reply(sess, FTP_UPLOADFAIL, "Could not creat file.");
                return;
        }

        int ret = 0;
	ret = lock_file_write(fd);

        if(ret == -1)
        {
                ftp_reply(sess, FTP_UPLOADFAIL, "Could not creat file.");
                return;
        }

	long long offset = sess->restart_pos;	
	sess->restart_pos = 0;

	if(!is_append && offset == 0)   //STOR
	{
		ftruncate(fd, 0);
		if(lseek(fd, 0, SEEK_SET) < 0)
		{
			ftp_reply(sess, FTP_UPLOADFAIL, "Could not creat file.");
                	return;
		}
	}

	else if(!is_append && offset != 0)  //REST + STOR
	{
		if(lseek(fd, offset, SEEK_SET) < 0)
		{
			ftp_reply(sess, FTP_UPLOADFAIL, "Could not creat file.");
                        return;
		}
	}

	else if(is_append)
	{
		if(lseek(fd, 0, SEEK_END) < 0)
		{
			ftp_reply(sess, FTP_UPLOADFAIL, "Could not creat file.");
                        return;
		}
	}
	
	struct stat sbuf;
        fstat(fd, &sbuf);

	char text[1024] = {0};

        if(sess->is_ascii)
        {
                sprintf(text, "Opening ASCII mode data conection for %s (%lld bytes).",
                sess->arg, (long long)sbuf.st_size);
        }
        else
        {
                sprintf(text, "Opening BINARY mode data conection for %s (%lld bytes).",
                sess->arg, (long long)sbuf.st_size);
        }

        ftp_reply(sess, FTP_DATACONN, text);

	int flag = 0;
	char buf[65536] = {0};

	sess->bw_transfer_start_sec = get_time_sec();
	sess->bw_transfer_start_usec = get_time_usec();

	while(1)
	{
		ret = read(sess->data_fd, buf, sizeof(buf));
		if(ret == -1)
		{
			if(errno == EINTR)
				continue;
			
			flag = 2;
			break;
		}
		
		else if(ret == 0)
		{
			flag = 0;
			break;
		}

		limit_rate(sess, ret, 1);

		if(sess->abor_received)	
		{
			flag = 2;	
			break;
		}

		if(writen(fd, buf, ret) != ret)
		{
			flag = 1;
			break;
		}	
	}	

	close(sess->data_fd);
	sess->data_fd = -1;
	close(fd);

	if(flag == 0)
		ftp_reply(sess, FTP_TRANSFEROK, "Transfer complete.");
	
	else if(flag == 1)
		ftp_reply(sess, FTP_BADSENDFILE, "Failure to write file.");
	
	else if(flag == 2)
		ftp_reply(sess, FTP_BADSENDNET, "Failure read from network stream.");

	check_abor(sess);
	start_cmdio_alarm();
}
Beispiel #8
0
static int
dump (void* user, const mdbm_iterate_info_t* info, const kvpair* kv)
{
    mdbm_dump_info_t* di = (mdbm_dump_info_t*)user;

    if (info->i_entry.entry_index == 0) {
        printf("P%06d/%06d # "
               "entries=%-5d (%6d bytes)  deleted=%-5d (%6d bytes)"
               "  free=%-6d (%6d total)"
               "  sizes=%d/%d/%d\n",
               info->i_page.page_num,
               info->i_page.mapped_page_num,
               info->i_page.page_active_entries,
               info->i_page.page_active_space,
               info->i_page.page_deleted_entries,
               info->i_page.page_deleted_space,
               info->i_page.page_free_space,
               info->i_page.page_free_space + info->i_page.page_deleted_space,
               info->i_page.page_min_entry_size,
               info->i_page.page_max_entry_size,
               info->i_page.page_active_entries
               ? (info->i_page.page_active_space / info->i_page.page_active_entries)
               : 0);
    }
    if (di->flags & MDBM_DUMP_PAGES_ONLY) {
        return 0;
    }
    if (!(info->i_entry.entry_flags & MDBM_ENTRY_DELETED) || di->flags & MDBM_DUMP_DELETED) {
        printf("  P%06d/%06d [E%04d] %s klen=%-5d vlen=%-5d koff=%-5d voff=%-5d",
               info->i_page.page_num,
               info->i_page.mapped_page_num,
               info->i_entry.entry_index,
               (info->i_entry.entry_flags & MDBM_ENTRY_DELETED)
               ? "D"
               : ((info->i_entry.entry_flags & MDBM_ENTRY_LARGE_OBJECT)
                  ? "L"
                  : " "),
               kv->key.dsize,
               kv->val.dsize,
               info->i_entry.key_offset,
               info->i_entry.val_offset);
        if (info->i_entry.entry_flags & MDBM_ENTRY_LARGE_OBJECT) {
            printf(" lob-page=%d/%p lob-pages=%d",
                   info->i_entry.lob_page_num,
                   (void*)info->i_entry.lob_page_addr,
                   info->i_entry.lob_num_pages);
        }
        if (di->flags & MDBM_DUMP_CACHEINFO) {
            printf("    num_accesses=%u access_time=%u (t-%lu) priority=%f",
                   info->i_entry.cache_num_accesses,
                   info->i_entry.cache_access_time,
                   info->i_entry.cache_access_time
                   ? (long)(get_time_sec() - info->i_entry.cache_access_time)
                   : 0,
                   info->i_entry.cache_priority);
        }
        putchar('\n');
        if (di->flags & MDBM_DUMP_KEYS) {
#if 0
            int p;
            MDBM_ITER iter;
            datum key, val;

            if ((p = mdbm_get_page(di->db,&kv->key)) != info->i_page.page_num) {
                printf("### ENTRY ON WRONG PAGE (should be %d)\n",p);
            }
            key = kv->key;
            if (mdbm_fetch_r(di->db,&key,&val,&iter) < 0) {
                printf("### UNABLE TO FETCH\n");
            }
#endif
            print_bytes("  K ",kv->key,1);
        }
        if (di->flags & MDBM_DUMP_VALUES) {
            print_bytes("  V ",kv->val,1);
        }
    }
    return 0;
}