分类目录归档:编程参考

一个GPU监视工具

这是一个用于监视NVIDIA GPU 训练的工具。可以防止训练进程卡死占用资源。
可以根据自己的情况调整位于第34行时间阈值。

import os
import subprocess
import time
import psutil

nvtask = {}

while True:

    nvrescmd = subprocess.run(['nvidia-smi','pmon','-c','1'], stdout=subprocess.PIPE)
    nvresout = nvrescmd.stdout.decode('utf-8')
    nvprocarr = nvresout.split('\n')[2:]
    for nvproc in nvprocarr:
        nvprocinfo = nvproc.split()
        if len(nvprocinfo) <2:
            continue
        if nvprocinfo[2] == 'G' or nvprocinfo[2] == '-':
            continue
        #print('gpu:{}, pid:{}, util:{}'.format(nvprocinfo[0],nvprocinfo[1],nvprocinfo[3]))
        if int(nvprocinfo[3]) == 0 : 
            if int(nvprocinfo[1]) in nvtask:
                nvtask[int(nvprocinfo[1])] += 1
            else:
                nvtask[int(nvprocinfo[1])] = 1
            if nvtask[int(nvprocinfo[1])] > 10:
                from termcolor import colored
                print('Process {} counted for {}'.format(nvprocinfo[1],nvtask[int(nvprocinfo[1])]))
        else:
            nvtask[int(nvprocinfo[1])] = 0
    for pid, zerocount in nvtask.copy().items():
        if not psutil.pid_exists(pid):
            del nvtask[pid]
        else:
            if zerocount > 300:
                os.kill(pid,9)
    os.system('nvidia-smi')
    os.system('sensors')
    time.sleep(1)
    os.system('clear')

C语言中的字符串替换

输入参数:char* search 需要搜索的字符串
char* replace 被替换的字符串
char* str 原始字符串
返回 char* 用完后用free()释放

[codesyntax lang=”c” lines=”normal”]

/*
*Function:str_replace
*Parameter:char* search,char* replace,char* str
*Call:char *str_replace(char* search,char* replace,char* str);
*Return: string 
*Required:malloc.h
*Required:string.h
*Description: replace the string in the string
*/char *str_replace(char* search,char* replace,char* str)
{
    int lstr,lse,lre;
    char* r,*p,*nptr;
    lse=strlen(search);
    lre=strlen(replace);
    lstr=strlen(str);
    if(lse>lstr)
    { 
        return NULL;
    }
    r=(char* )malloc(lstr+1);
    if(r==NULL)
    {
        printf("Failed to allocate memory");
        exit(-2);
    }
    strcpy(r,str);                    /*Copy the string to new memory*/
    p=strstr(r,search);
    while(p!=NULL)
    {
        if(lse==lre)
        {
            memcpy(p,replace,lre);  /*Just Copy the string*/
        }else if(lse>lre)            /*No allocation required*/
        {
            memset(p,' ',lse);        /*Clear it first*/
            memcpy(p,replace,lre);    /*Copy data*/
            memcpy(p+lre,p+lse,lstr-(p-r+lse)+1);/*Remove the blanks*/
        }else{
            nptr=realloc(r,lstr+(lre-lse));/*Expand the space first*/
            if(nptr==NULL)
            {
                printf("Failed to allocate memory");
                exit(-2);
            }
            r=nptr;
            memcpy(p+lre,p+lse,lstr-(p-r+lse)+1);/*Move data now*/
            memcpy(p,replace,lre);
        }
        p=strstr(p+lre,search);        /*To prevent the replace string contained the search string*/
    }
    return r;
}

[/codesyntax]