Beispiel #1
0
/// Create a thread and add it to Active Threads and set it to state READY
osThreadId osThreadCreate(osThreadDef_t *thread_def, void *argument)
{
    osThreadId thread;
    int size;

    size = thread_def->stack_size;
    if (size == 0)
    {
        size = 4096;
    }

    thread = rt_thread_create(thread_def->name, thread_def->entry, argument, size, thread_def->priority, thread_def->tick);
    if (thread != RT_NULL)
        rt_thread_startup(thread);

    return thread;
}
Beispiel #2
0
rt_err_t remote_init(const char * uart_name)
{
	uart = rt_device_find(uart_name);

	RT_ASSERT(uart != RT_NULL);

	rt_device_set_rx_indicate(uart, byte_recv);

	rt_device_open(uart, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_INT_RX);
	
	rt_sem_init(&remote_sem,"remote",0,RT_IPC_FLAG_FIFO);
	
	remote_thread = rt_thread_create("remote",remote_thread_entry,RT_NULL,512,9,3);

	rt_thread_startup(remote_thread);
	
	return RT_EOK;
}
Beispiel #3
0
void application_init()
{
	static rt_bool_t inited = RT_FALSE;

	if (inited == RT_FALSE) /* 避免重复初始化而做的保护 */
	{
		rt_thread_t tid;

		tid = rt_thread_create("wb",
			application_entry, RT_NULL,
			2048 * 2, 25, 10);

		if (tid != RT_NULL)
			rt_thread_startup(tid);

		inited = RT_TRUE;
	}
}
Beispiel #4
0
int ft5406_hw_init(void)
{
    rt_thread_t tid;
    rt_device_t dev;

    dev = rt_device_find(I2CBUS_NAME);
    if (!dev) return -1;

    if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
        return -1;

    FTDEBUG("ft5406 set i2c bus to %s\n", I2CBUS_NAME);
    _i2c_bus = (struct rt_i2c_bus_device *)dev;

    {
        gpio_pin_config_t pin_config =
        {
            kGPIO_DigitalOutput, 0,
        };

        CLOCK_EnableClock(kCLOCK_Gpio2);

        /* Enable touch panel controller */
        GPIO_PinInit(GPIO, 2, 27, &pin_config);
        GPIO_WritePinOutput(GPIO, 2, 27, 1);
        rt_thread_delay(50);
        GPIO_WritePinOutput(GPIO, 2, 27, 0);
        rt_thread_delay(50);
        GPIO_WritePinOutput(GPIO, 2, 27, 1);
    }

    rt_sem_init(&_tp_sem, "touch", 0, RT_IPC_FLAG_FIFO);
    tid = rt_thread_create("touch", _touch, RT_NULL,
                           2048, 10, 20);
    if (!tid)
    {
        rt_device_close(dev);
        return -1;
    }

    rt_thread_startup(tid);

    return 0;
}
Beispiel #5
0
int rt_application_init()
{
    rt_thread_t init_thread;


     Device_CAN2_regist();    //  Device CAN2 Init 

#if (RT_THREAD_PRIORITY_MAX == 32)
    init_thread = rt_thread_create("init",
                                   rt_init_thread_entry, RT_NULL,
                                   256, 8, 20);  // thread null 
#endif

    if (init_thread != RT_NULL)
        rt_thread_startup(init_thread);

   return 0; 

}
Beispiel #6
0
void rt_application_init(void)
{
    rt_thread_t tid;

#ifdef RT_USING_HEAP
    tid = rt_thread_create("main", main_thread_entry, RT_NULL,
                           2048, RT_THREAD_PRIORITY_MAX / 3, 20);
    RT_ASSERT(tid != RT_NULL);
#else
    rt_err_t result;

    tid = &main_thread;
    result = rt_thread_init(tid, "main", main_thread_entry, RT_NULL,
                            2048, RT_THREAD_PRIORITY_MAX / 3, 20);
    RT_ASSERT(result != RT_EOK);
#endif

    rt_thread_startup(tid);
}
sys_thread_t sys_thread_new(const char    *name,
                            lwip_thread_fn thread,
                            void          *arg,
                            int            stacksize,
                            int            prio)
{
    rt_thread_t t;

    RT_DEBUG_NOT_IN_INTERRUPT;

    /* create thread */
    t = rt_thread_create(name, thread, arg, stacksize, prio, 20);
    RT_ASSERT(t != RT_NULL);

    /* startup thread */
    rt_thread_startup(t);

    return t;
}
Beispiel #8
0
int rt_application_init(void)
{
    rt_thread_t init_thread = NULL;
    rt_thread_init(&thread_sys_monitor,
                   "sys_monitor",
                   thread_entry_sys_monitor,
                   RT_NULL,
                   thread_sys_monitor_stack,
                   sizeof(thread_sys_monitor_stack),
                   thread_sys_monitor_prio, 5);
    rt_thread_startup(&thread_sys_monitor);

    init_thread = rt_thread_create("sys init", sys_init_thread,
            NULL, 512, 10, 10);
    if (init_thread != NULL) {
        rt_thread_startup(init_thread);
    }
    return 0;
}
Beispiel #9
0
rt_err_t appNRF24L01Init(void)
{
	rt_err_t status;
	rt_thread_t init_thread;
		
	status = RF24L01_Check();
	if(status == RT_EOK) {
		init_thread = rt_thread_create("appNRF24L01",
															 rt_appNRF24L01_thread_entry, RT_NULL,
															 2048, 8, 20);
		if (init_thread != RT_NULL)
			rt_thread_startup(init_thread);
		else
			status = RT_ERROR;
	}else
		status = RT_ERROR;
	
	return status;
}
void USB_cable(void)
{
    rt_device_t dev = RT_NULL;
    SD_CardInfo * sdio_info = RT_NULL;
    dev = rt_device_find("sd0");

  
    if(dev != RT_NULL)
    {
        dev_sdio = dev;
        sdio_info = (SD_CardInfo *)dev->user_data;
        Mass_Memory_Size[0] = sdio_info->CardCapacity;
        Mass_Block_Size[0]  = 512;//sdio_info->CardBlockSize;
        Mass_Block_Count[0] = Mass_Memory_Size[0] / Mass_Block_Size[0];
    }
    else
    {
        rt_kprintf("\r\nNo find the device sd0 !!!!");
    }
    /* 3:NAND */
	
	

    /* usb msc up*/
    Set_System();
    Set_USBClock();
    USB_Interrupts_Config();
    USB_Init();

#if (USB_USE_AUTO_REMOVE == 1)
    /* delete the other thread */

    /* create msc_thread */
    {
        rt_thread_t msc_thread;
        msc_thread = rt_thread_create("msc_thread",
                                      msc_thread_entry, RT_NULL,
                                      1024, RT_THREAD_PRIORITY_MAX-1,1);
        if (msc_thread != RT_NULL) rt_thread_startup(msc_thread);
    }
#endif
}
Beispiel #11
0
int main(void)
{
	int i, k, prio, bprio;

	task[0] = rt_task_init_schmod(0, NTASKS - 1, 0, 0, SCHED_FIFO, CPUS_ALLOWED);
	mlockall(MCL_CURRENT | MCL_FUTURE);
	rt_make_hard_real_time();

	for (i = 0; i < NTASKS; i++) {
		sem[i] = rt_typed_sem_init(0, RESEMT, RES_SEM);
	}

	rt_sem_wait(sem[0]);

        for (i = 1; i < NTASKS; i++) {
		rt_thread_create(tskfun, (void *)i, 0);
		rt_receive(NULL, &k);
		rt_printk("AFTER TSKNR %d CREATED > (TSKNR-PRI):\n", i);
        	for (k = 0; k < i; k++) {
			rt_get_priorities(task[k], &prio, &bprio);
			rt_printk("%d-%d|", k, prio);
		}
		rt_get_priorities(task[i], &prio, &bprio);
		rt_printk("%d-%d\n\n", i, prio);
	}

	rt_sem_signal(sem[0]);

	rt_printk("FINAL > (TSKNR-PRI):\n");
       	for (k = 0; k < (NTASKS - 1); k++) {
		rt_get_priorities(task[k], &prio, &bprio);
		rt_printk("%d-%d|", k, prio);
	}
	rt_get_priorities(task[NTASKS - 1], &prio, &bprio);
	rt_printk("%d-%d\n\n", (NTASKS - 1), prio);

	for (i = 0; i < NTASKS; i++) {
		rt_sem_delete(sem[i]);
	}

	return 0;
}
// rtt应用程序初始化
void rt_application_init(void)
{
    rt_thread_t tid;

#ifdef RT_USING_HEAP    // 此处已开启
    // 此处进入main函数中   创建了一个主任务入口
    tid = rt_thread_create("main", main_thread_entry, RT_NULL,
                           2048, RT_THREAD_PRIORITY_MAX / 3, 20);
    RT_ASSERT(tid != RT_NULL);
#else
    rt_err_t result;

    tid = &main_thread;
    result = rt_thread_init(tid, "main", main_thread_entry, RT_NULL,
                            2048, RT_THREAD_PRIORITY_MAX / 3, 20);
    RT_ASSERT(result != RT_EOK);
#endif

    rt_thread_startup(tid); // 启动当前创建的任务,在调度器启动后才会真正的调用
}
Beispiel #13
0
void rt_hw_adc_init(void)
{
	adc.type 		= RT_Device_Class_Char;
	adc.rx_indicate = RT_NULL;
	adc.tx_complete = RT_NULL;
	adc.init 		= rt_adc_init;
	adc.open		= RT_NULL;
	adc.close		= RT_NULL;
	adc.read 		= RT_NULL;
	adc.write 		= RT_NULL;
	adc.control 	= rt_adc_control;
	adc.user_data	= RT_NULL;

    adc_thread = rt_thread_create("adc", adc_thread_entry, RT_NULL, 384, 26, 5);
    if(adc_thread != RT_NULL) 
        rt_thread_startup(adc_thread);
    
	/* register a character device */
	rt_device_register(&adc, "adc", RT_DEVICE_FLAG_RDWR);
}
int rs485_system_init(void)
{
    rt_err_t result;
    rt_thread_t init_thread;

	rs485_send_mut = rt_mutex_create("rs485mut",RT_IPC_FLAG_FIFO);
		
	rs485_data_init();	
	


    uart1_dev_my = (struct _uart_dev_my*)rt_malloc(sizeof(struct _uart_dev_my));
    if (uart1_dev_my == RT_NULL)
    {
        rt_kprintf("no memory for shell\n");
        return -1;
    }
    memset(uart1_dev_my, 0, sizeof(struct _uart_dev_my));
    rt_sem_init(&(uart1_dev_my->rx_sem), "rs485rx", 0, 0);
	uart1_dev_my->device = RT_NULL;
	uart1_rs485_set_device();

	
	
	
	uart1_sem = rt_sem_create("uart1_sem",0, RT_IPC_FLAG_FIFO);  


		init_thread = rt_thread_create("rs485",rt_rs485_thread_entry, RT_NULL,
                                   4092, 8, 21);
	  if (init_thread != RT_NULL)
        rt_thread_startup(init_thread);


//    init_thread = rt_thread_create("rsdecode",rt_rs485_decode_thread_entry, RT_NULL,
//                                   512, 8, 21);
//    if (init_thread != RT_NULL)
//        rt_thread_startup(init_thread);

    return 0;
}
Beispiel #15
0
void calibration_init(void)
{
    rt_thread_t tid;

    rt_device_t device = rt_device_find("touch");

    if (device == RT_NULL)
    {
        rt_kprintf("RTGUI: no touch device to calibrate\n");
        return;
    }

    if (_cali_restore && (_cali_restore()==RT_TRUE))
    {
        return;
    }

    tid = rt_thread_create("cali", calibration_entry, RT_NULL, 1024, 20, 20);
    if (tid != RT_NULL)
        rt_thread_startup(tid);
}
Beispiel #16
0
int rt_application_init(void)
{
    rt_thread_t tid;
    rt_err_t result;
    tid = rt_thread_create("init",
                           rt_init_thread_entry, RT_NULL,
                           2048, 3, 20);
    if (tid != RT_NULL)
        rt_thread_startup(tid);

    /* init led thread */
    result = rt_thread_init(&led_thread, "led",
                            led_thread_entry, RT_NULL,
                            (rt_uint8_t *)&led_stack[0], sizeof(led_stack),
                            20, 5);
    if (result == RT_EOK)
    {
        rt_thread_startup(&led_thread);
    }
    return 0;
}
void Commander::Init(const char *name)
{
	rt_thread_t recv_thread;
	dev  = rt_device_find(name);
	if(dev == RT_NULL)
  {
	 rt_kprintf("can not find %s!\n", name);
		return ;
	}
	recv_event = rt_event_create("cmdrecv",RT_IPC_FLAG_FIFO);
	if(recv_event==RT_NULL)
  {
	 rt_kprintf("commander recv event create error!\n");
		return ;
	}
	rt_device_set_rx_indicate(dev,data_rx_callback);
	rt_device_open(dev,RT_DEVICE_FLAG_INT_RX|RT_DEVICE_FLAG_RDWR);
	recv_thread = rt_thread_create("cmd_recv",recv_thread_entry,RT_NULL,1024,12,5);
	if(recv_thread != RT_NULL)
		rt_thread_startup(recv_thread);
}
Beispiel #18
0
int rt_application_init()
{
    rt_thread_t init_thread;

    init_thread = rt_thread_create("init",
                                   rt_init_thread_entry, RT_NULL,
                                   2048, RT_THREAD_PRIORITY_MAX/3, 20);
    if (init_thread != RT_NULL)
        rt_thread_startup(init_thread);

    //------- init led1 thread
    rt_thread_init(&thread_led1,
                   "led_demo",
                   rt_thread_entry_led1,
                   RT_NULL,
                   &thread_led1_stack[0],
                   sizeof(thread_led1_stack),11,5);
    rt_thread_startup(&thread_led1);

    return 0;
}
Beispiel #19
0
int termios_test(int argc, char **argv)
{
    rt_thread_t tid;

    if(argc < 2)
    {
        rt_kprintf("Please input device name...\n");
        return -1;
    }

    term_param.dev = argv[1];
    term_param.baud_rate = ((argc >= 3) ? atoi(argv[2]) : Default_baud_rate);

    tid = rt_thread_create("termtest",
        termios_test_entry, (void *)&term_param,
        512, RT_THREAD_PRIORITY_MAX/3, 20);

    if (tid != RT_NULL)
        rt_thread_startup(tid);

    return 0;
}
Beispiel #20
0
int rt_application_init()
{
    rt_thread_t tid;

    tid = rt_thread_create("init",
        rt_init_thread_entry, RT_NULL,
        2048, RT_THREAD_PRIORITY_MAX/3, 20);

    if (tid != RT_NULL)
        rt_thread_startup(tid);
#if 0		
		//------- init led2 thread
    rt_thread_init(&thread_led2,
                   "led2",
                   rt_thread_entry_led2,
                   RT_NULL,
                   &thread_led2_stack[0],
                   sizeof(thread_led2_stack),11,5);
    rt_thread_startup(&thread_led2);
#endif
    return 0;
}
Beispiel #21
0
int LCDPANELProcessInit(void)
{
	rt_thread_t init_thread;

	IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);			// Enable write access to IWDG_PR and IWDG_RLR registers
	IWDG_SetPrescaler(IWDG_Prescaler_32);					// IWDG counter clock: 40KHz(LSI) / 32 = 1.25 KHz 
	IWDG_SetReload(1250);									// Set counter reload value to 1250 
	IWDG_ReloadCounter();									// Reload IWDG counter 
	IWDG_Enable();											// Enable IWDG (the LSI oscillator will be enabled by hardware) 

	init_thread = rt_thread_create( "lcdpanel",
									LCDPANELProcess_thread_entry,
									RT_NULL,
									4096,20,10);

	if( init_thread != RT_NULL )
	{
		rt_thread_startup(init_thread);
	}

	return 0;
}
int manager_tasks(void)
{
	int contIdTask = 0;

	rt_set_periodic_mode();
	rt_make_hard_real_time();

	printf("************** Iniciando escalonamento **************\n");

	//rt_set_oneshot_mode();
	start_rt_timer(0);
	tick_period = nano2count(TICK_PERIOD); // 0.05 segundos...
	delay_start_timeline = tick_period * 20; // Delay: 2 segundo(s)
	start_timeline = rt_get_time() + delay_start_timeline;

	printf("TICK_PERIOD ================> %llu\n", tick_period);

	/**
	 * TASK(S)
	 */
	contIdTask = 0;//TASK:0
	arrayThreadParams[contIdTask].idTask = contIdTask;
	arrayThreadParams[contIdTask].periodo = tick_period * 320; // ~= 16 segundos
//	arrayThreadParams[contIdTask].deadline = 0.0013;
	arrayThreadParams[contIdTask].deadline = arrayThreadParams[contIdTask].periodo;
	arrayThreadParams[contIdTask].prioridade = 4;
//	arrayThreadParams[contIdTask].cpuFrequencyMin = VALOR_DEFAULT_FREQ_MIN_DESABILITADA; // KHz
	arrayThreadParams[contIdTask].cpuFrequencyMin = 3000000; // KHz
	arrayThreadParams[contIdTask].cpuFrequencyInicial = 3000000; // KHz
	arrayThreadParams[contIdTask].cpuVoltageInicial = AMD_ATHLON_II_X2_250_TENSAO_FREQ_1800000_KHZ; // Volts
	Thread_Task = rt_thread_create(init_task, &arrayThreadParams[contIdTask], 0);

	// Aguarda interrupcao do usuario... ou a conclusao dos periodos de todas as tarefas criadas...
	while(!getchar());

	stop_rt_timer();
	return 0;
}
Beispiel #23
0
int main(void)
{
	RT_TASK *mytask;
	RT_MSGQ *smbx;
	int bthread;
	char msg[] = "let's end the game";

	mytask = rt_thread_init(nam2num("MAIN"), 2, 0, SCHED_FIFO, 0xF);

	smbx = rt_msgq_init(nam2num("SMSG"), 0, 0);
	bthread = rt_thread_create(bfun, 0, 0x8000);

	printf("IF NOTHING HAPPENS IS OK, TYPE ENTER TO FINISH.\n");
	getchar();

	end = 1;
	rt_msg_send(smbx, msg, sizeof(msg), 1);
	rt_thread_join(bthread);

	rt_task_delete(mytask);
	printf("MAIN TASK ENDS.\n");
	return 0;
}
Beispiel #24
0
void calibration_init()
{
	rt_device_t device;

	device = rt_device_find("touch");
	if (device == RT_NULL)
        return; /* no such device */

    touch_screen_calibrated = rt_sem_create("tc_cali", 0, RT_IPC_FLAG_FIFO);
    if (touch_screen_calibrated == RT_NULL)
        return;

	calibration_ptr = (struct calibration_session*)rt_malloc(sizeof(struct calibration_session));
	rt_memset(calibration_ptr, 0, sizeof(struct calibration_data));
	calibration_ptr->device = device;

	rt_device_control(calibration_ptr->device, RT_TOUCH_CALIBRATION, (void*)calibration_data_post);

	calibration_ptr->tid = rt_thread_create("cali", calibration_entry, RT_NULL,
		2048, 20, 5);
	if (calibration_ptr->tid != RT_NULL)
        rt_thread_startup(calibration_ptr->tid);
}
Beispiel #25
0
int wavplay(int argc, char **argv)
{
    rt_thread_t tid = RT_NULL;

    if (argc != 2)
    {
        printf("Usage:\n");
        printf("wavplay song.wav\n");
        return 0;
    }

    memset(file_name, 0, sizeof(file_name));
    memcpy(file_name, argv[1], strlen(argv[1]));

    tid = rt_thread_create("wayplay",
                           wavplay_thread_entry,
                           RT_NULL,
                           1024 * 8,
                           22,
                           10);
    if (tid != RT_NULL)
        rt_thread_startup(tid);
}
Beispiel #26
0
sys_thread_t sys_thread_new(char *name, void (* thread)(void *arg), void *arg, int stacksize, int prio)
{
	rt_thread_t t;
	struct lwip_thread* lwip_th;

	/* create lwip thread */
	lwip_th = (struct lwip_thread*) rt_malloc (sizeof(struct lwip_thread));
	RT_ASSERT(lwip_th != RT_NULL);

	/* create thread */
	t = rt_thread_create(name, thread, arg, stacksize, prio, 20);
	RT_ASSERT(t != RT_NULL);
	t->user_data = (rt_uint32_t)lwip_th;

	/* init lwip_thread */
	lwip_th->magic = LWIP_THREAD_MAGIC;
	lwip_th->timeouts.next = RT_NULL;
	lwip_th->tid = t;

	/* startup thread */
	rt_thread_startup(t);

	return t;
}
Beispiel #27
0
int rt_application_init(void)
{
    rt_thread_t tid;
    rt_err_t  result;
    tid = rt_thread_create("init",
                           rt_init_thread_entry, RT_NULL,
                           2048, RT_THREAD_PRIORITY_MAX / 3, 20);
    if (tid != RT_NULL) rt_thread_startup(tid);

    /* init led thread */
//    result = rt_thread_init(&led_thread,
//                            "led",
//                            led_thread_entry,
//                            RT_NULL,
//                            (rt_uint8_t*)&led_stack[0],
//                            sizeof(led_stack),
//                            20,
//                            5);
//    if (result == RT_EOK)
//    {
//        rt_thread_startup(&led_thread);
//    }
    return 0;
}
Beispiel #28
0
/**
 * This function will create a rms object and allocate rms object memory and stack
 */
rt_rms_t rt_rms_create(const char *name,
                       void (*entry)(void *parameter),
                       void *parameter,
                       rt_uint32_t stack_size,
                       rt_uint32_t tick,
                       rt_uint8_t period,
                       rt_uint8_t wcet)
{
    struct rt_rms  *rms;

    rms = (struct rt_rms *)rt_object_allocate(RT_Object_Class_Thread, name);

    if(rms->thread == RT_NULL)
        return RT_NULL;

    /* create a thread object */
#ifndef RT_RMS_ACCURACY
#define RT_RMS_ACCURACY 1
#endif
    rms->thread = rt_thread_create(name, entry, parameter, stack_size, period / RT_RMS_ACCURACY, tick);
    _rt_rms_init(rms, period, wcet);

    return rms;
}
Beispiel #29
0
int calc_grid_energies_excl_mgrid(float* atoms, float* grideners, long int numplane, long int numcol, long int numpt, long int natoms, float gridspacing, unsigned char* excludepos, int maxnumprocs) {
// cionize_params* params, cionize_molecule* molecule, cionize_grid* grid) {
  int i;
  enthrparms *parms;
  rt_thread_t * threads;

#if defined(THR)
  int numprocs;
  int availprocs = rt_thread_numprocessors();
  if (params->maxnumprocs <= availprocs) {
    numprocs = params->maxnumprocs;
  } else {
    numprocs = availprocs;
  }
#else
  int numprocs = 1;
#endif

  printf("calc_grid_energies_excl_mgrid()\n");

  /* DH: setup and compute long-range */
  MgridParam mg_prm;
  MgridSystem mg_sys;
  Mgrid mg;
  //MgridLattice *lattice;
  int j, k;
  int gridfactor;

  double h, h_1;
  int nspacings;

  double *eh;
  int ndim;
  int ii, jj, kk;
  int im, jm, km;
  int ilo, jlo, klo;
  double dx_h, dy_h, dz_h;
  double xphi[4], yphi[4], zphi[4];
  double t, en, c;
  int koff, jkoff, index;

  rt_timerhandle timer;
  float totaltime;

  memset(&mg_prm, 0, sizeof(mg_prm));
  memset(&mg_sys, 0, sizeof(mg_sys));

  /*
   * set mgrid parameters
   *
   * make sure origin of mgrid's grid is at ionize's grid origin (0,0,0)
   *
   * length is (number of grid spacings) * (grid spacing),
   * where the number of spacings is one less than number of grid points
   */
  mg_prm.length = (numpt-1) * gridspacing;
  mg_prm.center.x = 0.5 * mg_prm.length;
  mg_prm.center.y = 0.5 * mg_prm.length;
  mg_prm.center.z = 0.5 * mg_prm.length;

  /* make sure domain and grid are both cubic */
  if (numpt != numcol || numcol != numplane) {
    printf("ERROR: grid must be cubic\n");
    return -1;
  }

  /*
   * grid used by mgrid needs spacing h >= 2
   *
   * determine grid factor:  (2^gridfactor)*h_ionize = h_mgrid
   */
  gridfactor = 0;
  //nspacings = numpt - 1;
  nspacings = numpt;
    /* add one more spacing so that interpolation loop below will work */
  h = gridspacing;
  while (h < 2.0) {
    h *= 2;
    nspacings = ((nspacings & 1) ? nspacings/2 + 1 : nspacings/2);
    gridfactor++;
  }
  mg_prm.nspacings = nspacings;

  /* have to modify mgrid length */
  mg_prm.length += h;
  mg_prm.center.x = 0.5 * mg_prm.length;
  mg_prm.center.y = 0.5 * mg_prm.length;
  mg_prm.center.z = 0.5 * mg_prm.length;
  h_1 = 1.0/h;

  mg_prm.cutoff = CUTOFF;
  mg_prm.boundary = MGRID_NONPERIODIC;

  /* choice of splitting must be consistent with short-range below */
#if defined(CUBIC_TAYLOR2)
  mg_prm.approx = MGRID_CUBIC;
  mg_prm.split = MGRID_TAYLOR2;
#elif defined(QUINTIC1_TAYLOR3)
  mg_prm.approx = MGRID_QUINTIC1;
  mg_prm.split = MGRID_TAYLOR3;
#elif defined(HEPTIC1_TAYLOR4)
  mg_prm.approx = MGRID_HEPTIC1;
  mg_prm.split = MGRID_TAYLOR4;
  /*
#elif defined(HERMITE_TAYLOR3)
  mg_prm.approx = MGRID_HERMITE;
  mg_prm.split = MGRID_TAYLOR3;
  */
#endif

  mg_prm.natoms = natoms;
  printf("natom = %d\n", mg_prm.natoms);
  printf("mgrid center = %g %g %g\n",
      mg_prm.center.x, mg_prm.center.y, mg_prm.center.z);
  printf("mgrid length = %g\n", mg_prm.length);
  printf("mgrid nspacings = %d\n", mg_prm.nspacings);

  /* setup mgrid system */
  mg_sys.f_elec = (MD_Dvec *) calloc(mg_prm.natoms, sizeof(MD_Dvec));
  mg_sys.pos = (MD_Dvec *) calloc(mg_prm.natoms, sizeof(MD_Dvec));
  mg_sys.charge = (double *) calloc(mg_prm.natoms, sizeof(double));
  for (i = 0;  i < mg_prm.natoms;  i++) {
    mg_sys.pos[i].x  = atoms[4*i    ];
    mg_sys.pos[i].y  = atoms[4*i + 1];
    mg_sys.pos[i].z  = atoms[4*i + 2];
    mg_sys.charge[i] = atoms[4*i + 3];
  }

  /* setup mgrid solver and compute */
  if (mgrid_param_config(&mg_prm)) {
    printf("ERROR: mgrid_param_config() failed\n");
    return -1;
  }
  printf("spacing = %g\n", mg_prm.spacing);
  if (mgrid_init(&mg)) {
    printf("ERROR: mgrid_init() failed\n");
    return -1;
  }
  if (mgrid_setup(&mg, &mg_sys, &mg_prm)) {
    printf("ERROR: mgrid_setup() failed\n");
    return -1;
  }

  timer = rt_timer_create();
  rt_timer_start(timer);

  if (mgrid_force(&mg, &mg_sys)) {
    printf("ERROR: mgrid_force() failed\n");
    return -1;
  }
  /* DH: end setup and compute long-range */

  printf("  using %d processors\n", numprocs);  

  /* allocate array of threads */
  threads = (rt_thread_t *) calloc(numprocs * sizeof(rt_thread_t), 1);

  /* allocate and initialize array of thread parameters */
  parms = (enthrparms *) malloc(numprocs * sizeof(enthrparms));
  for (i=0; i<numprocs; i++) {
    parms[i].threadid = i;
    parms[i].threadcount = numprocs;
    parms[i].atoms = atoms;
    parms[i].grideners = grideners;
    parms[i].numplane = numplane;
    parms[i].numcol = numcol;
    parms[i].numpt = numpt;
    parms[i].natoms = natoms;
    parms[i].gridspacing = gridspacing;
    parms[i].excludepos = excludepos;
  }

#if defined(THR)
  /* spawn child threads to do the work */
  for (i=0; i<numprocs; i++) {
    rt_thread_create(&threads[i], energythread, &parms[i]);
  }

  /* join the threads after work is done */
  for (i=0; i<numprocs; i++) {
    rt_thread_join(threads[i], NULL);
  } 
#else
  /* single thread does all of the work */
  energythread((void *) &parms[0]);
#endif

  /* DH: tabulate and cleanup long-range */

  /* interpolate from mgrid potential lattice */

  eh = (double *)(mg.egrid[0].data); /* mgrid's long-range potential lattice */
  ndim = mg.egrid[0].ni;  /* number of points in each dimension of lattice */

  for (kk = 0;  kk < numplane;  kk++) {
    for (jj = 0;  jj < numcol;  jj++) {
      for (ii = 0;  ii < numpt;  ii++) {

        /* distance between atom and corner measured in grid points */
        dx_h = (ii*gridspacing) * h_1;
        dy_h = (jj*gridspacing) * h_1;
        dz_h = (kk*gridspacing) * h_1;

        /* find closest mgrid lattice point less than or equal to */
        im = ii >> gridfactor;
        jm = jj >> gridfactor;
        km = kk >> gridfactor;

#if defined(CUBIC_TAYLOR2)
        ilo = im-1;
        jlo = jm-1;
        klo = km-1;

        /* find t for x dimension and compute xphi */
        t = dx_h - ilo;
        xphi[0] = 0.5 * (1 - t) * (2 - t) * (2 - t);
        t--;
        xphi[1] = (1 - t) * (1 + t - 1.5 * t * t);
        t--;
        xphi[2] = (1 + t) * (1 - t - 1.5 * t * t);
        t--;
        xphi[3] = 0.5 * (1 + t) * (2 + t) * (2 + t);

        /* find t for y dimension and compute yphi */
        t = dy_h - jlo;
        yphi[0] = 0.5 * (1 - t) * (2 - t) * (2 - t);
        t--;
        yphi[1] = (1 - t) * (1 + t - 1.5 * t * t);
        t--;
        yphi[2] = (1 + t) * (1 - t - 1.5 * t * t);
        t--;
        yphi[3] = 0.5 * (1 + t) * (2 + t) * (2 + t);

        /* find t for z dimension and compute zphi */
        t = dz_h - klo;
        zphi[0] = 0.5 * (1 - t) * (2 - t) * (2 - t);
        t--;
        zphi[1] = (1 - t) * (1 + t - 1.5 * t * t);
        t--;
        zphi[2] = (1 + t) * (1 - t - 1.5 * t * t);
        t--;
        zphi[3] = 0.5 * (1 + t) * (2 + t) * (2 + t);

        /* determine 64=4*4*4 eh grid stencil contribution to potential */
        en = 0;
        for (k = 0;  k < 4;  k++) {
          koff = (k + klo) * ndim;
          for (j = 0;  j < 4;  j++) {
            jkoff = (koff + (j + jlo)) * ndim;
            c = yphi[j] * zphi[k];
            for (i = 0;  i < 4;  i++) {
              index = jkoff + (i + ilo);
              /*
              ASSERT(&eh[index]
                  == mgrid_lattice_elem(&(mg.egrid[0]), i+ilo, j+jlo, k+klo));
                  */
              if (&eh[index] !=
                  mgrid_lattice_elem(&(mg.egrid[0]), i+ilo, j+jlo, k+klo)) {
                printf("ndim=%d  index=%d  i+ilo=%d  j+jlo=%d  k+klo=%d\n"
                    "ia=%d  ib=%d  ni=%d\n"
                    "ja=%d  jb=%d  nj=%d\n"
                    "ka=%d  kb=%d  nk=%d\n",
                    ndim, index, i+ilo, j+jlo, k+klo,
                    mg.egrid[0].ia, mg.egrid[0].ib, mg.egrid[0].ni,
                    mg.egrid[0].ja, mg.egrid[0].jb, mg.egrid[0].nj,
                    mg.egrid[0].ka, mg.egrid[0].kb, mg.egrid[0].nk
                    );
                abort();
              }

              en += eh[index] * xphi[i] * c;
            }
          }
        }
        /* end CUBIC */
#endif

        //ENERGY(grid->eners[k*numcol*numpt + j*numpt + i]);

        grideners[kk*numcol*numpt + jj*numpt + ii] += (float)en;

        //  (float) *((double *)mgrid_lattice_elem(lattice, i, j, k));

        //ENERGY((float) *((double *)mgrid_lattice_elem(lattice, i, j, k)));
        //ENERGY(grid->eners[k*numcol*numpt + j*numpt + i]*560.47254);
      }
    }
  }

  totaltime = rt_timer_timenow(timer);
  printf("total time for mgrid: %.1f\n", totaltime);
  rt_timer_destroy(timer);

  /* cleanup mgrid */
  mgrid_done(&mg);
  free(mg_sys.f_elec);
  free(mg_sys.pos);
  free(mg_sys.charge);
  /* DH: tabulate and cleanup long-range */

  /* free thread parms */
  free(parms);
  free(threads);

  return 0;
}
Beispiel #30
0
int main(int argc, char *argv[])
{
	unsigned long sndnode;
	long sndport, i, r;
	RT_TASK *rcvtsk, *sndtsk;
        struct sockaddr_in addr;
	static MBX *sndmbx;

	SERVER = atoi(argv[1]);
 	if (!(rcvtsk = rt_task_init_schmod(nam2num("RCVTSK"), 2, 0, 0, SCHED_FIFO, 0xF))) {
		printf("CANNOT INIT RECEIVER TASK\n");
		exit(1);
	}

        mbx = rt_mbx_init(nam2num("MBX"), 100);
        athread = rt_thread_create(async_fun, NULL, 10000);
        sndnode = 0;
        if (argc == 3 && strstr(argv[2], "SndNode=")) {
                inet_aton(argv[2] + 8, &addr.sin_addr);
		sndnode = addr.sin_addr.s_addr;
        }
        if (!sndnode) {
                inet_aton("127.0.0.1", &addr.sin_addr);
                sndnode = addr.sin_addr.s_addr;
        }

	rt_set_oneshot_mode();
	start_rt_timer(0);
	while ((sndport = rt_request_port_mbx(sndnode, mbx)) <= 0 && sndport != -EINVAL);
        while (!(sndmbx = RT_get_adr(sndnode, sndport, "SNDMBX"))) {
                rt_sleep(nano2count(100000000));
        }
	sndtsk = RT_get_adr(sndnode, sndport, "SNDTSK");
	printf("\nRECEIVER TASK RUNNING\n");
	mlockall(MCL_CURRENT | MCL_FUTURE);
	rt_make_hard_real_time();

	while (!end) {	
		r = RT_mbx_receive(sndnode, -sndport, sndmbx, &i, sizeof(long));
		rt_printk("RECEIVE %ld %ld\n", r, i);
if (SERVER) {
		rt_sleep(nano2count(100000000)); 
} else {
		while (!end && rt_waiting_return(sndnode, sndport)) {
			rt_sleep(nano2count(1000000)); 
		}
		if (!end) {
			unsigned long long retval;
			long i1, i2;
			int l1, l2;
			if (rt_sync_net_rpc(sndnode, -sndport)) {
				l1 = l2 = sizeof(long);
				rt_get_net_rpc_ret(mbx, &retval, &i1, &l1, &i2, &l2, 0LL, MBX_RECEIVE);
				rt_printk("RECEIVER ASYNC MSG: RETVAL = %d, MSG = %ld, LEN = %d.\n", (int)retval, i1, l1);
				if (i1 < 0) {
					end = 1;
					break;
				}
			}
		}
}
	}

	rt_mbx_delete(mbx);
	rt_release_port(sndnode, sndport);
	rt_make_soft_real_time();
	rt_thread_join(athread);
	rt_return(rt_receive(0, &i), i);
	rt_task_delete(rcvtsk);
	stop_rt_timer();
	printf("\nRECEIVER TASK STOPS\n");
	return 0;
}