skip to Main Content

I have a dockerized flask application. I wish to run the following code as multiprocessing but right now it is utilizing CPU cores and the container is crashing. What should be done to make sure it utilizes the GPU instead of the CPU? Any modifications to the code or the container?

 processes = []
    for i in range(len(session['optitower_files'])):
        try:
            print("_________________________________Started Processess__________________________________________")
            p = Process(target= generate_optimizer_formula, kwargs={"data":optitower_data_frames[f'data_{i}'],"save_file_paths":save_file_paths[i], "return_data":return_data[f'data_{i}'],"brd_folder_path":brd_folder_path,"user_session_path":user_session_path,"meta_data":meta_data,"legacy":legacy})
            processes.append(p)
            p.start()
            # data,data2     = return_data
    
        except Exception as e:
            print(e)
            pass
            # data_dict               = read_from_json(loc=save_file_paths["Optimizer Status"])
            # data_dict["Optimizer"]  = "Error Occured; Optimizer Stopped"
            # write_to_json(data_dict,save_file_paths["Optimizer Status"])  
    for p in processes:
        p.join()

My Docker File:

FROM company_flask_app_v2_backup
RUN bash
ENV DEBIAN_FRONTEND=noninteractive

RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1
WORKDIR /company

COPY ./requirements/requirements.txt .
COPY ./requirements/requirements.py .

RUN pip3 install -r requirements.txt
RUN python3 requirements.py

CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "app_dev.py"]

Docker File (Initiatory):

FROM ubuntu_image_v2

WORKDIR /company

ENV DEBIAN_FRONTEND=noninteractive

COPY ./requirements/requirements.txt .
COPY ./requirements/requirements.py .
RUN apt-get update

RUN apt-get install -y 
    software-properties-common

RUN apt-get update &&  add-apt-repository universe
RUN add-apt-repository ppa:deadsnakes/ppa
RUN apt-get update &&  apt-get install -y 
    python3.11 
    python3-pip

RUN apt install -y wkhtmltopdf
RUN pip3 install -r requirements.txt

RUN apt-get update &&  apt-get install latexmk -y --fix-missing
RUN apt-get install texlive-latex-extra -y
RUN apt-get install texlive-fonts-recommended -y

RUN apt-get update && 
    apt-get install -y curl gnupg unixodbc

RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - && 
    curl https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/prod.list > /etc/apt/sources.list.d/mssql-release.list

RUN apt-get update && 
    ACCEPT_EULA=Y apt-get install -y msodbcsql18
    
RUN apt-get clean && 
    rm -rf /var/lib/apt/lists/*


RUN apt-get update &&  apt-get install libreoffice -y --fix-missing

2

Answers


  1. I would suggest using an image based on nvidia/cuda.

       FROM nvidia/cuda:11.2.2-cudnn8-runtime-ubuntu20.04
        
        ENV DEBIAN_FRONTEND=noninteractive
        
        # Install Python and other dependencies
        RUN apt-get update && apt-get install -y 
            python3.8 
            python3-pip 
            && rm -rf /var/lib/apt/lists/*
        
        # Set Python 3.8 as the default
        RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
        
        WORKDIR /company
        
        COPY ./requirements/requirements.txt .
        COPY ./requirements/requirements.py .
        
        RUN pip3 install -r requirements.txt
        RUN python3 requirements.py
        
        CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "app_dev.py"]
    

    Also if you want to run docker with gpu support :

    docker run --gpus all <image_name>
    

    in case you want to access all gpus available.

    Login or Signup to reply.
  2. I was using tensorflow but pytorch is more good and so instead of multiprocessing, use pytorch

    import torch  
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    
    def generate_optimizer_formula(data, save_file_paths, return_data, brd_folder_path, user_session_path, meta_data, legacy):
        data = data.to(device)
        ........
    

    Also use image nivdia/cuba:11.3-base

    FROM nvidia/cuda:11.3-base  
    
    RUN apt-get update && apt-get install -y python3.11 python3-pip
    WORKDIR /company
    
    COPY ./requirements/requirements.txt .
    RUN pip3 install -r requirements.txt
    
    CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "app_dev.py"]
    

    Run docker with flag –gpus for GPU resources

    docker run --gpus all -p 5000:5000 your_image
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search