在Phi与host之间使用socket

Phi是intel推出的x86协处理器,运行轻量级linux,使用PCI-E与主机通信。但是,Phi上的print需要等到程序运行完才能显示,等程序运行完,黄花菜都凉了。而且瞬间几百行输出,谁有心思看啊。由此就有这个问题:实时显示。之前做过这方面的 调研。不过是单机上的,这种类多机的没有试过,简单的解决方案是用socket,而双方都是linux,很容易开发。

Host:

#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include "statics.h"
#include <sys/time.h>

int main()
{
        long sec = 0;
        long usec = 0;

        struct timeval begin,end;
        gettimeofday(&begin, NULL);
        int fixSock, accSock;
        struct sockaddr_in srvrAdd, clntAdd;
        int sin_size;
        unsigned short portnum=0x8888;
        message msg;

        printf("Hello,welcome to my server !\r\n");
        fixSock = socket(AF_INET, SOCK_STREAM, 0);
        if(-1 == fixSock)
        {
                perror("socket fail");
                return -1;
        }
        printf("socket ok !\r\n");
        int on;
        setsockopt(fixSock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));

        bzero(&srvrAdd,sizeof(struct sockaddr_in));
        srvrAdd.sin_family=AF_INET;
        srvrAdd.sin_addr.s_addr=htonl(INADDR_ANY);
        srvrAdd.sin_port=htons(portnum);

        if(-1 == bind(fixSock,(struct sockaddr *)(&srvrAdd), sizeof(struct sockaddr)))
        {
                perror("bind fail");
                return -1;
        }
        printf("bind ok !\r\n");

        if(-1 == listen(fixSock,5))
        {
                perror("listen fail");
                return -1;
        }
        printf("listen ok\r\n");

        sin_size = sizeof(struct sockaddr_in);
        gettimeofday(&end, NULL);
        sec = end.tv_sec - begin.tv_sec;
        usec = end.tv_usec - begin.tv_usec;

        accSock = accept(fixSock, (struct sockaddr *)(&clntAdd), &sin_size);

        gettimeofday(&begin, NULL);
        if(-1 == accSock)
        {
                printf("accept fail !\r\n");
                return -1;
        }
        printf("accept ok!\r\nServer start get connect from %#x : %#x\r\n",ntohl(clntAdd.sin_addr.s_addr),ntohs(clntAdd.sin_port));

        while(1){
                if(-1 == read(accSock, &msg, sizeof(msg)))
                {
                        perror("write fail");
                        return -1;
                }

                if(0 == msg.type){
                        int *arr = (int*)msg.data;
                        if(msg.length == 1){
                                if(9999 == arr[0]){
                                        break;
                                }
                        }
                }
        }
        close(accSock);
        close(fixSock);
        gettimeofday(&end, NULL);
        printf("%ld s, %ld us", end.tv_sec - begin.tv_sec + sec, end.tv_usec - begin.tv_usec + usec);
        return 0;
}

Phi:

#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include "statics.h"

int mConnect(char *ip){
        int sockid;
        struct sockaddr_in sera, clia;
        unsigned short port = 0x8888;

        sockid = socket(AF_INET, SOCK_STREAM, 0);
        if( -1 == sockid){
                return -1;
        }
        bzero(&sera, sizeof(struct sockaddr_in));
        sera.sin_family = AF_INET;
        sera.sin_addr.s_addr = inet_addr(ip);
        sera.sin_port = htons(port);

        if(-1 == connect(sockid, (struct sockaddr *)(&sera), sizeof(struct sockaddr))){
                return -1;
        }
        return sockid;
}
int mSend(int sockid, message *msg){
        int bytes;
        if(-1 == ( bytes = write( sockid, msg, sizeof(message) ) )){
                return -1;
        }
        return bytes;
}
int main()
{
        int sockid;

        sockid = mConnect("10.4.14.100");
        message msg;
        printf("Hello,welcome to client !\r\n");
        msg.type = 0;
        msg.length = 1;
        for(int i = 0; i < 10000; ++i){
                ((int*)msg.data)[0] = i;
                mSend(sockid, &msg);
        }
        getchar();
        close(sockid);
        return 0;
}

测试性能也和上次一样,发10000次数据看时间。

38630
37492
38840
37680
77976
36907
AVG 44587.5
VAR 268073450.3
MID 38155
性能与本机的shm和fifo比还是有一定差距的。


你可能感兴趣的:(在Phi与host之间使用socket)